我正在运行 Firebase add
在 map 内。问题是我需要返回新添加文档的引用,但看起来它会进入 .then
之前的下一个迭代当前迭代的运行。我尝试过 await
但我无法让它正常工作。
taskContexts = taskContexts.map(taskContext => {
const found = contexts.find(el => el.name === taskContext);
// If context exists, return reference
if (found) {
return this.props.firebase.db.doc("/contexts/" + found.uid);
} else {
// If it doesn't, create new context
// The add works, but it looks like the .then is only ran after the iteration has finished
this.props.firebase.contexts().add({
created: this.props.firebase.fieldValue.serverTimestamp(),
name: taskContext,
})
// Then return reference
.then((response) => {
console.log("New context added with id:");
console.log(response.id);
return this.props.firebase.db.doc("/contexts/" + response.id);
})
.catch(function(error) {
console.error("Error adding new context: ", error);
});
}
});
最佳答案
关于javascript - Firebase 添加内部 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061228/