【问题标题】:Firestore merge does not return a PormiseFirestore 合并不返回 Promise
【发布时间】:2019-03-05 05:24:25
【问题描述】:

我正在根据https://firebase.google.com/docs/firestore/manage-data/add-data 此处的指导方针写信给 Firestore,并设置 merge:true 选项,但是,这不会返回承诺吗?因此,我们无法在打开此标志的情况下链接 .then().catch()

我们如何确定它已经完成了设置/合并操作呢?

谢谢

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    {merge: true} 选项与set() 方法一起使用不会改变该方法在返回内容方面的行为。正如documentation 中所解释的,该方法确实返回了一个 Promise

    返回: 包含 void 的非空 Promise。一个承诺,一旦解决 数据已成功写入后端。 (注意不会 离线时解决)。

    所以,除非你处于离线状态,否则你可以很好地执行以下操作:

    db.collection("cities").doc("LA").set({
        name: "Los Angeles",
        state: "CA",
        country: "USA"
    },
    {merge: true})
    .then(function() {
        // ->>> Here you can be sure that "it has completed the set/merge operation"
        console.log("Document successfully written!");
    })
    .catch(function(error) {
        console.error("Error writing document: ", error);
    });
    

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 2020-11-28
      • 2022-01-04
      • 2020-08-24
      • 2020-01-25
      • 2020-07-11
      • 2022-08-19
      • 2017-10-31
      • 2020-01-07
      相关资源
      最近更新 更多