cantaş
cantaş

Reputation: 136

Getting data from Firebase real-time on react-native

I have a js file that is initializing the firebase database, I want to connect to the firebase realtime database and fetch the relevant information.

The function I'm calling:

get getName() {
var ref = firebase.database().ref('variable').child('variable').child('variable');

ref.on('value', gotData, errData);



function gotData(data) {

    console.log(data.val());
    console.log(data.val().ogrIsim + 'getname calisti');
    console.log(String(data.val().ogrIsim));
    this.setState({
        name: (data.val().ogrIsim)
    });
    // return String(data.val().ogrIsim);
}

function errData(err) {
    console.log('errorrrr!!!' + err);
}

}

console.log (data.val (). [variableName]); I print console as a string in the line, but I can't change it because it says there is an object that is not a string in setstate e. How should I proceed?

and the error message :

enter image description here

Upvotes: 0

Views: 566

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

The error message happens because this has a different meaning inside the gotData function than outside of it.

There are many ways to address this, but in this case an explicit bind() might be easiest:

var ref = firebase.database().ref('variable').child('variable').child('variable');

ref.on('value', gotData, errData);

function gotData(data) {
  this.setState({ name: (data.val().ogrIsim) });
}.bind(this);
function errData(err) {
  console.log('errorr' + err);
}

For much more on this, see How to access the correct `this` inside a callback?

Upvotes: 1

Related Questions