0

Firebase data is as follows

-users
--demo1
---conid:1
-election
--election1
---conRegex:1
--election2
---conRegex:1

the code to retrieve election1, election2 is

var conid;
    var conRegex;
    var electionArr = [];
    if(uidAnonymous != null) {
        db.ref('users/'+user).once('value', function(snapshot) {
            if(snapshot.exists()) {
                conid = snapshot.val().conid;
            }
        });
        db.ref('election/').once('value', function(snapshot) {
            snapshot.forEach(function(electionSnapshot) {
                conRegex = electionSnapshot.val().conRegex;
                if(conid.startsWith(conRegex)) {
                    electionArr.push(electionSnapshot.key.toString());
                }
            });
        });
        console.log(electionArr);
    }

Problem is electionArr is empty? How do i solve the issue

2 Answers 2

2

Firebase function calls are asynchronous. Thats how it works you cannot assign an array from out of scope inside firebase on function call. Make your array available inside your once(value) function.

var conid;
var conRegex;
if(uidAnonymous != null) {
    db.ref('users/'+user).once('value', function(snapshot) {
        if(snapshot.exists()) {
            conid = snapshot.val().conid;
        }
    });
    db.ref('election/').once('value', function(snapshot) {
        var electionArr = [];
        snapshot.forEach(function(electionSnapshot) {
            conRegex = electionSnapshot.val().conRegex;
            if(conid.startsWith(conRegex)) {
                Object.keys(electionSnapshot.val()).map(k => {
                    electionArr.push(electionSnapshot.val()[k]);
                })
            }
        });
        console.log(electionArr);
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

i need electionArr outside the if(uidAnonymous != null) then how will i do that?
I have edited the code. You needs to map through keys of returned snapshot object and push values to array
I have edited the code tell me If data is coming in electionArr now or not then we try further. Because I am unable to check the output
Not getting the expected results that is the electionSnapshot.key
what do you mean by expected output. Are you even getting values in electionSnapshot.val().
|
1

The once() function is asynchronous. You'll have to nest your reads and log your array to the console when all the data is read:

var conid;
var conRegex;
var electionArr = [];
if(uidAnonymous != null) {
    db.ref('users/'+user).once('value', function(snapshot) {
        if(snapshot.exists()) {
            conid = snapshot.val().conid;

            db.ref('election/').once('value', function(snapshot) {
                snapshot.forEach(function(electionSnapshot) {
                    conRegex = electionSnapshot.val().conRegex;
                    if(conid.startsWith(conRegex)) {
                        electionArr.push(electionSnapshot.key.toString());
                    }
                });
                console.log(electionArr);
            });
        }
    });
}

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.