React Native

Read Data on your React Native App

Introduction

In this section you will learn how to fetch data from Back4App Database to your React Native App.

Prerequisites

To complete this tutorial, you will need:

Step 1 - Reading an Object

Saving data to the Back4App Database is simple, but it’s even easier to get that data out again. If the Parse.Object has been uploaded to the server, you can use the objectId to get it using a Parse.Query:

Let’s say you want to retrieve the Jonh Snow data you have stored on your Social Media App. Go to your Back4App App Dashboard and copy the objectId.

1
   objectId: b90N1cYpR8

Step 2 - Create a Read Function

Now let’s create a simple function inside the App.js file to retrieve this objectIdinformation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
retrievePerson = async () => {
    
  var Person = Parse.Object.extend("Person");
  var query = new Parse.Query(Person);

  query.get("b90N1cYpR8")
      .then((person) => {
        var name = person.get("name");
        var age = person.get("age");

      // The object was retrieved successfully.
      alert('Name: ' + name + ' age: '+age) ;
      }, (error) => {
      // The object was not retrieved successfully.
      // error is a Parse.Error with an error code and message.
       alert('Failed to retrieve the object, with error code: ' + error.message);
      });
 

}   

Note that the query.get method returned a Person object then we use the .get("columnNameHere") to retrieve each attribute.

There are also four special objects that cannot be read using the getmethod:

1
2
3
4
var objectId = person.id;
var updatedAt = person.updatedAt;
var createdAt = person.createdAt;
var acl = person.getACL();

If you need to refresh the person object you already have with the latest data that is in the Back4App Cloud, you can call the fetch method like so:

1
2
3
4
5
6
person.fetch().then((person) => {
  // The object was refreshed successfully.
}, (error) => {
  // The object was not refreshed successfully.
  // error is a Parse.Error with an error code and message.
});

If you need to check if the object has been fetched, you can call the isDataAvailable() method:

1
2
3
4
if (!person.isDataAvailable()) {
  await person.fetch();
}

Step 3 - Call the Function

Now let’s call our function. You can do it using another single Button inside your React Native Application as shown below. We’ve inserted the Read Button just below the Save Button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>React Native on Back4App</Text>
        <Text style={styles.instructions}>Reading objects tutorial</Text>
     
        <TouchableOpacity onPress={() => this.saveNewPerson() } style={styles.button}>
        <Text style={styles.buttonText}>Save</Text>
        
      </TouchableOpacity>
      <TouchableOpacity onPress={() => this.retrievePerson() } style={styles.button}>
        <Text style={styles.buttonText}>Read</Text>
        
      </TouchableOpacity>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

Your App will look like this:

React Native Back4App

Then press Read and you’ll see the confirmation message.

React Native Save Data Confirmation

It’s done!

At this point, you have Saved and Read your first data on Back4App. On the next tutorial we’ll show you how to Update it.