Jean
Jean

Reputation: 201

retrieve the contents of the firebase database

I created this simple function (according to this guide: https://medium.com/@jamesmarino/getting-started-with-react-native-and-firebase-ab1f396db549)

export function getNameOfUserToKill(userID, gameID, callback) {
  let path = '/games/' + gameID + '/' + userID;
  fb
    .database()
    .ref(path)
    .on('value', snapshot => {
      var nameOfUserToKill = '';

      if (snapshot.val()) {
        nameOfUserToKill = snapshot.val().userToKill;
      }

      callback(nameOfUserToKill);
    });
}

But I don't know how to finally get the data... Should I do nameOfUserToKill = getNameOfUserToKill(USER1234, GAME65754) ? I don't know what "callback" means. Thank you for your help.


So I can do it:

var nameOfUserToKill = '';

export function getNameOfUserToKill(userID, gameID) {
  let path = '/games/' + gameID + '/' + userID;
  fb
    .database()
    .ref(path)
    .on('value', snapshot => {
      nameOfUserToKill = '';

      if (snapshot.val()) {
        nameOfUserToKill = snapshot.val().userToKill;
      }
    });
}

var user = nameOfUserToKill;

Upvotes: 1

Views: 60

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

To retrieve the content of the Firebase Database:

 let path = '/games/' + gameID + '/' + userID;
fb.database().ref(path).on('value', snapshot => {
  var nameOfUserToKill = '';
  if (snapshot.val()) {
    nameOfUserToKill = snapshot.val().userToKill;
  }

nameofUserToKill will contain the data that is retrieved.

Upvotes: 1

Related Questions