如何在执行 Firestore 操作后清理资源,我想在保存记录后使用“finally” block 关闭对话框,但它提示它不是一个函数。 我一直在搜索 API 引用,但我只找到入门部分中的几个示例。
我的代码是这样的:
db.collection("posts")
.doc(doc.id)
.set(post)
.then(function(docRef) {
//todo
})
.catch(function(error) {
console.error("Error saving post : ", error);
})
/*.finally(function(){
//close pop up
})*/
;
最佳答案
节点 6 中的 native Promise 没有 finally() 方法。只有 then() 和 catch()。 ( See this table ,节点在最右边。)
如果您想在 promise 链的末尾无条件地做某事,而不管成功或失败,您可以在 then() 和 catch() 回调中复制它:
doSomeWork()
.then(result => {
cleanup()
})
.catch(error => {
cleanup()
})
function cleanup() {}
或者您可以使用 TypeScript,它在语言中定义了 try/catch/finally。
关于javascript - Firestore Set Catch 最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389163/