javascript - Firestore Set Catch 最后

标签 javascript firebase promise google-cloud-functions google-cloud-firestore

如何在执行 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/

相关文章:

javascript - 如何查询兄弟节点的现有子节点并循环它们以检测其是否存在?

mysql - 如何修复发送到客户端后无法设置 header ?

javascript - 将变量从 Promise 转移到 Promise

javascript - Promise `finally` 不起作用

javascript - 使用 javascript 为具有类名的 div 设置填充

javascript - Appscript 中的授权请求 header

javascript - 是否有检测 Firefox 的 "good practice"方法?

javascript - Json Schema 对 Value 字段的限制,可以是 Integer 或 String

swift - addSnapshotListener 快速 Firestore 行为

firebase - Cloud Firestore 测试期结束后会发生什么