检索Firebase数据并将其返回的最佳方式,或者其他方式 [英] Best way to retrieve Firebase data and return it, or an alternative way
问题描述
我只是偶然发现了一件我不能做的事情,想知道是否有办法实现它,或者有另一种方法来实现它。 p>
我试图做的是创建一个函数来从我的Firebase数据库中检索最后X个条目并将数据返回给调用函数。 b
$ b
类似于这样:
$ p $ function getTwoLatestLocations()
{
var twoLocations;
myFirebaseRef.limitToLast(2).once('value',function(dataSnapshot){
twoLocations = dataSnapshot.val();
},function(errorObject){
/ / code来处理读取错误
console.log(The read failed:+ errorObject.code);
});
返回twoLocations;
}
然后调用它:
函数someCalcutationsWithTheTwoLastLocations()
{
var twoLocation = getTwoLatestLocations();
//做一些计算
}
但是正如您可能已经猜到调用数据库引用是异步的,所以返回的对象将永远是未定义的。
有没有办法做到这一点,所以我可以保持方法分开?
PS目前我所有的代码都只是在调用数据库的时候。丑陋的。
要将代码分离出来,您需要传递一个回调:
function getTwoLatestLocations(callback)
{
var twoLocations;
myFirebaseRef.limitToLast(2).once('value',function(dataSnapshot){
twoLocations = dataSnapshot.val();
callback(twoLocations);
},function (errorObject){
//处理读取错误的代码
console.log(读取失败:+ errorObject.code);
});
然后像这样调用它:
函数someCalcutationsWithTheTwoLastLocations()
{
getTwoLatestLocations(function(twoLocations){
//做一些计算
});
$ b $ p
$ b 要记住的重要一点是你不能等待加载的东西异步。
已经涵盖了很多次,包括从相关问题列表的顶部答案:在函数中处理异步调用(Firebase)
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.
解决方案 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
这篇关于检索Firebase数据并将其返回的最佳方式,或者其他方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!