Jaimin Modi
Jaimin Modi

Reputation: 1677

Function inside a Function not calling in React Native

I am new to react-native and calling a function inside a fucntion.

I have done as below so far :

Step 1 : Created a function _snapshotToArray to convert the firebase snapshot to Arrray.

_snapshotToArray(snapshot) {
var returnArr = [];

snapshot.forEach(function(childSnapshot) {
    var item = childSnapshot.val();
    item.key = childSnapshot.key;

    returnArr.push(item);
});
return returnArr;
 }

Step 2 : Created another function as below and calling _snapshotToArray inside it.

_readUserDataFromFirebaseConsole() {//once and on
firebase.database().ref('Users/').on('value', function (snapshot) {
    console.log(this._snapshotToArray(snapshot));
    Toast.show(this._snapshotToArray(snapshot),Toast.LONG);
});
}

Talking about this call :

console.log(this._snapshotToArray(snapshot));

When I press CTRL+CLick, it not letting me to navigate to body of the fuction _snapshotToArray.

In Device am getting below error :

_snapshotToArray is not defined

What might be the issue ?

Upvotes: 0

Views: 126

Answers (1)

Device
Device

Reputation: 567

I'm not at my PC right now, so I cannot test it, but from looking at your code, you need to use a different function notation to allow the varibale access of/from parent methods and parent class.

_snapshotToArray = snapshot => {
    var returnArr = [];

    snapshot.forEach(function(childSnapshot) {
        var item = childSnapshot.val();
        item.key = childSnapshot.key;

        returnArr.push(item);
    });
    return returnArr;
}

and

_readUserDataFromFirebaseConsole = () => {
    firebase.database().ref('Users/').on('value', snapshot => {
        console.log(this._snapshotToArray(snapshot));
        Toast.show(this._snapshotToArray(snapshot),Toast.LONG);
    });
}

Upvotes: 1

Related Questions