最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to retrieve Firebase data and return it, or an alternative way - Stack Overflow

programmeradmin1浏览0评论

I am using Firebase for some projects and am liking the platform a lot.

I just stumbled upon a thing that I could not do and was wondering if there was any way to achieve it or if there was an alternative way of doing it.

What I am trying to do is to create a function to retrieve the last X number of entries from my Firebase database and returning the data to the calling function.

Something like this:

function getTwoLatestLocations()
{
    var twoLocations;
    myFirebaseRef.limitToLast(2).once('value', function (dataSnapshot) {
        twoLocations = dataSnapshot.val();
    }, function (errorObject) {
        // code to handle read error
        console.log("The read failed: " + errorObject.code);
    });

    return twoLocations;
}

And then calling it like this:

function someCalcutationsWithTheTwoLastLocations()
{
    var twoLastLocations = getTwoLatestLocations();
    // Do some calculations
}

But as you might have guessed the call to the database reference is asynchronous so the returning object will always be undefined.

Is there a way to do this elegantly so I can keep the methods separated?

P.S. Currently all my code is simply inside the call to the database. Ugly.

I am using Firebase for some projects and am liking the platform a lot.

I just stumbled upon a thing that I could not do and was wondering if there was any way to achieve it or if there was an alternative way of doing it.

What I am trying to do is to create a function to retrieve the last X number of entries from my Firebase database and returning the data to the calling function.

Something like this:

function getTwoLatestLocations()
{
    var twoLocations;
    myFirebaseRef.limitToLast(2).once('value', function (dataSnapshot) {
        twoLocations = dataSnapshot.val();
    }, function (errorObject) {
        // code to handle read error
        console.log("The read failed: " + errorObject.code);
    });

    return twoLocations;
}

And then calling it like this:

function someCalcutationsWithTheTwoLastLocations()
{
    var twoLastLocations = getTwoLatestLocations();
    // Do some calculations
}

But as you might have guessed the call to the database reference is asynchronous so the returning object will always be undefined.

Is there a way to do this elegantly so I can keep the methods separated?

P.S. Currently all my code is simply inside the call to the database. Ugly.

Share Improve this question edited Jan 20, 2016 at 19:10 Frank van Puffelen 600k85 gold badges890 silver badges860 bronze badges asked Jan 20, 2016 at 16:39 SigmundurSigmundur 8971 gold badge10 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

To separate the code out, you'd pass in a callback:

function getTwoLatestLocations(callback)
{
    var twoLocations;
    myFirebaseRef.limitToLast(2).once('value', function (dataSnapshot) {
        twoLocations = dataSnapshot.val();
        callback(twoLocations);
    }, function (errorObject) {
        // code to handle read error
        console.log("The read failed: " + errorObject.code);
    });

}

And then calling it like this:

function someCalcutationsWithTheTwoLastLocations()
{
    getTwoLatestLocations(function(twoLocations) {
        // Do some calculations
    });
}

The important thing to always remember is that you cannot wait for something that is loaded asynchronously.

This has been covered quite a few times, including in this top answer from the list of related questions: Handling Asynchronous Calls (Firebase) in functions

发布评论

评论列表(0)

  1. 暂无评论