admin管理员组文章数量:1356304
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 badges1 Answer
Reset to default 8To 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
本文标签: javascriptBest way to retrieve Firebase data and return itor an alternative wayStack Overflow
版权声明:本文标题:javascript - Best way to retrieve Firebase data and return it, or an alternative way - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005180a2574569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论