3

I declared a global array in index.js (firebase function). Once the code is deployed, this array is filled from firebase data. I have two functions, in the first one (onTW) i made some changes to the array, and i'm just displaying it in the other function(onRemoveTW). The problem is I'm getting an empty array in the second function. Here's my code.

    var TWArray = [];
    TWRef.once('value', function (snapshot) {
snapshot.forEach(function(childSnapshot) {
    var name=childSnapshot.key;
    var users = {};
    var userNbr = 0;
    TWRef.child(name).child('rm').once('value', function (snapshot2) {
        snapshot2.forEach(function(childSnapshot2) {
            userNbr++;
            if(childSnapshot2.key=='a'){
                users.a = childSnapshot2.val();
            }
            if(childSnapshot2.key=='b'){
                users.b = childSnapshot2.val();
            }
            if(childSnapshot2.key=='c'){
                users.c = childSnapshot2.val();
            }
            if(childSnapshot2.key=='d'){
                users.d = childSnapshot2.val();
            }
        })
        TWArray.push({
            rmName:name,
            users:users,
            userNbr:userNbr
        });
    })

})
    })
    exports.onTW = functions.database
    .ref('/Orders/TW/{requestId}')
    .onWrite(event => {
const userKey = event.data.key;
const post = event.data.val();
 if (post != null) {
    var users={};
            users.a=userKey;
            TWArray.push({
                rmName:userKey,
                users:users,
                userNbr:1
            });
            console.log(TWArray);
            console.log("TWArray.length : "+TWArray.length);
     }
    });
     exports.onRemoveTW = functions.database
     .ref('/Orders/RemoveTW/{requestId}')
    .onWrite(event => {
const userKey = event.data.key;
const post = event.data.val();
if (post != null) {

    console.log("TWArray.length : "+TWArray.length);
}
    })

Thanks in advance!

1 Answer 1

3

You cannot share data between functions by writing to global variables when using firebase-functions, because they intended to be stateless. As such, this functionality is not supported.

What you can do is write your data to firebase-database instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.