【问题标题】:adding data on firestore在 Firestore 上添加数据
【发布时间】:2018-10-31 20:36:41
【问题描述】:

我正在尝试将数据保存在 Firestore 数据库中

db.collection('clients').doc(clientInformation['id']).set(clientInformation)
        .then( result => {
            res.status(200).send({ client: result });
        });

如果提供的 id 已经存在于 db 上,我想要得到一个错误,我知道 firestore 会覆盖它或为您提供合并此数据的选项,但它可能会收到使用不同的重复键的错误方法而不是“设置”并抓住它?

谢谢!

【问题讨论】:

    标签: javascript google-cloud-firestore


    【解决方案1】:

    首先尝试获取您要保存的特定信息。它不存在,写它,否则更新它。

    docRef.get().then(function(doc) {
        if (doc.exists) {
            console.log("Document already exists:", doc.data());
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document we can .set it");
        }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多