我正在尝试使用Promise.all()同时执行许多Firebase Cloud Storage查询:
exports.onNewOrder =
functions.database.ref('/orders/{orderId}').onCreate(event => {
const data = event.val();
const orderedBy = data.ordered_by;
const creations = data.creations;
var promises = []
for (var entry of creations.entries()) {
var key = entry[0], value = entry[1];
var creationPromise = admin.database().ref("creations/"+ key);
creationPromise.once("value")
.then((crSnapshot)=>{
const crName = crSnapshot.val().name;
const created = crSnapshot.val().created;
return "crName+'\n'+created”;
}).catch((error) => {
console.log("creation details error : "+ error);
});
promises.push(creationPromise);
}
const userPromise = admin.database().ref("users/"+ orderedBy);
userPromise.once("value")
.then((snapshot) => {
var name = snapshot.val().name;
var email = snapshot.val().email;
const userDetails = name+'\n'+email;
return userDetails;
}).catch((error) => {
console.log("user details error :"+ error);
});
promises.push(userPromise);
return Promise.all(promises).then(results => {
console.log("results: " + results)
return true;
}).catch((error) => {
console.log("sendMail failed : "+ error);
});
});
我希望看到每个已执行的Promise的完整查询结果,但是我得到的却是以下内容:
结果:
https://....firebaseio.com/creations/cr_1bfd5f16212f44c792a2cbe3e9c6e4cc
https://....firebaseio.com/creations/cr_343c363c2a214487ba8cb7101653f6af
https://....firebaseio.com/creations/cr_e3f7a0bafd1542ac898d96e86155b94e
https://....firebaseio.com/users/us_5e4c28e18e63454c861caa5c4fe6a6f0
我想念什么吗?
答案 0 :(得分:2)
以下应该可以解决问题。
详细的here:
Promise.all(iterable)
方法返回一个Promise 当iterable
参数中的所有诺言都具有时解析 已解决或iterable
参数不包含诺言的情况。它 拒绝的原因是第一个拒绝的承诺。
因此,您需要向promises
数组添加一些承诺。
once()
方法确实返回了承诺,如文档中的here所述。
exports.onNewOrder =
functions.database.ref('/orders/{orderId}').onCreate(event => {
const data = event.val();
const orderedBy = data.ordered_by;
const creations = data.creations;
const promises = []
for (var entry of creations.entries()) {
var key = entry[0];
var creationPromise = admin.database().ref("creations/"+ key).once("value");
promises.push(creationPromise);
}
const userPromise = admin.database().ref("users/"+ orderedBy).once("value");
promises.push(userPromise);
return Promise.all(promises)
.then(results => {
console.log("results: " + results);
return true;
//actually here you will probably do something else than just write the results to the console.
//In this case you should return a promise (instead of true).
}).catch((error) => {
console.log("sendMail failed : "+ error);
return false;
});
});