数组中的 Firebase 承诺未及时解决 [英] Firebase Promises in array not resolving in time
问题描述
我正在尝试从我的 firebase 数据库返回 3 个承诺,一旦所有三个承诺都实现了,我基本上想呈现一个新页面或做任何事情.所以我做了一个 Promise.All(...) 但之后我的列表仍然是空的.
I am trying to return 3 promises from my firebase DB and once all three promises have been fulfilled I basically want to render a new page or do whatever. So I do a Promise.All(...) but my lists are still empty afterwards.
我的查询是正确的,因为当我在这些函数中的每一个中使用 console.log() 时,我都会从我的数据库中获取对象,但我的 Promise.All 没有等待这些承诺解决,而是在Promise.All 返回空列表.
My queries are correct because when I console.log() within each of those functions, I get the objects returned from my DB but my Promise.All isn't waiting for those promises to resolve and instead executes the code within the Promise.All which is returning empty lists.
app.get('...', function (req, res) {
//Return first promise from DB save to zone_obj list
var zone_key = req.params.id;
var zone_obj = [];
firebase.database().ref(...).once('value').then((snapme) => {
zone_obj.push(snapme.val());
});
//Return second promise from DB save to members list
var members = [];
firebase.database().ref(...).on("value", function (snapshott) {
snapshott.forEach((snapper) => {
members.push(snapper.val());
});
});
//Return third promise from DB save to experiences list
var experiences = [];
firebase.database().ref(...).on("value", function (snapshot) {
snapshot.forEach((snap) => {
firebase.database().ref(...).once("value").then((snapit) => {
experiences.push(snapit);
});
});
});
//once all promises have resolved
Promise.all([experiences,zone_obj,members]).then(values => {
console.log(values[0]); //returns []
console.log(values[1]); //returns []
console.log(values[2]); //returns []
});
});
推荐答案
这实际上不是 firebase 的问题.Firebase 方法 .on("value")
实际上是一个将绑定到 Firebase 以获取实时更新的侦听器,这实际上不是一个承诺,每次数据时都会调用您的回调函数该节点上的更改.因此,如果您只想保存或获取数据一次,请使用 firebase.database().ref(...).set()
和 firebase.database().ref(...).once()
方法.
This is not actually firebase's problem. Firebase method .on("value")
is actually a listener that will be bound to firebase to get real-time updates and that is not actually a promise and your callback function will be called every time when data on that node is changed. so if you want to save or get data only once use firebase.database().ref(...).set()
and firebase.database().ref(...).once()
method respectively.
根据 firebase 文档
According to firebase docs
方法
on(eventType, callback, cancelCallbackOrContext, context) returns function()
一次方法
once(eventType, successCallback, failureCallbackOrContext, context) returns firebase.Promise containing any type
因此将您的代码更改为以下
So change your code to following
app.get('...', function (req, res) {
var promises = []
//Return first promise from DB save to zone_obj list
promises.push(firebase.database().ref(...).once('value'));
//Return second promise from DB save to members list
promises.push(firebase.database().ref(...).once('value'));
//Return third promise from DB save to experiences list
promises.push(firebase.database().ref(...).once('value'));
//once all promises have resolved
Promise.all(promises).then(values => {
console.log(values[0]); // zone_obj
console.log(values[1]); // members
console.log(values[2]); // experiences
});
});
这篇关于数组中的 Firebase 承诺未及时解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!