1

I'm trying to retrieve some Data from Firebase (which works perfectly), but my colleague needs that I return the value of a snapshot as an Array, so that he can use it in another file. And as far as I know, it's impossible knowing that the value event is Asynchronous.

here's an example:

function getData() {
let current = [];
qRef.on("value", function (snapshot) { //qRef is the reference to my DB
    current = snapshot.val();
}, function (errorObject) {
    console.log("The read failed: " + errorObject.code);
});
return current; //expected value is the returned data from the snapshot
}

Is there any alternative solution? Thanks.

2
  • Inside the value event it returns the data as I want it.. But in the return statement I only have an empty array, which is normal because it's executed before the asynchronous event. Commented Aug 11, 2018 at 18:24
  • Return snapshot.val() in the value function Commented Aug 11, 2018 at 18:25

2 Answers 2

4

You're right, you can't directly return the value because it's asynchronous.

Instead, you should make use of Promises:

function getData() {
  return qRef
    .once('value')
    .then(snapshot => snapshot.val())
    .then(value => [ value ])
}

Notice that I'm using .once() instead of .on(). .on() is for listening for changes, .once() is for just retrieving the value on-demand. Also, .on() doesn't use promises because a promise means "some async work will happen, then you'll get a result". Listening for a value might mean the callback is triggered many times.

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

3 Comments

Thanks a lot it works! I have just one issue, if I want to return an edited array of the snapshot, where can I add it? (Because I actually need 10 shuffled values of the snapshot)
The function in the second .then is taking the value, and sticking it in an array. You can do any manipulation to the snapshot value you'd like in that function.
Hi, im new with firebase+ nodejs. Why when i console.log(getData()); Its return Promise { <pending> }. Any advise?
0

if its not a snapshot , suppose a get function,then you can use this:-

function getData() {
    return new Promise(function(resolve, reject){
        let current = [];
        qRef.on("value", function (snapshot) { //qRef is the reference to my DB
            current = snapshot.val();
            resolve(current);
        }, function (errorObject) {
            console.log("The read failed: " + errorObject.code);
            reject({
                error: true
            })
        });

    })
}

call it like

getData.then(function(current){
    desired_variable = current
})

since its a snapshot you can pass a callbak function and call it everytime. callback function will set the data to the desired variable

 function getData(callback) {
    let current = [];
    qRef.on("value", function (snapshot) { //qRef is the reference to my DB
        current = snapshot.val();
        // write logic for current array
        callback(current);
    }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
        reject({
            error: true
        })
    });
}

it will be called like

getData(function(current){
       desired_variable = current;
})

hope it solves your issue

1 Comment

Perfect! Thanks.

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.