【问题标题】:Write data to document on creation在创建时将数据写入文档
【发布时间】:2019-02-21 16:14:53
【问题描述】:

我是 Firebase 云功能的新手,每次创建文档时我都会尝试在文档中写下数据,但从未写入数据,我在控制台中也没有出现错误。我错过了什么?我正在使用 Firestore。

exports.updateclient = functions.firestore
  .document('patients/{clientId}')
  .onCreate(async (snap, context) => {
    const database = admin.firestore();
    const settings = {timestampsInSnapshots: true};
    database.settings(settings);
    const clientId= context.params.clientId;
    const patientRef = database.collection('patient').doc(clientId);
    return patientRef.set({ id: clientId}, {merge: true});
  });

【问题讨论】:

    标签: javascript typescript firebase google-cloud-firestore


    【解决方案1】:

    正如 Doug 已经提到的,您尝试在不同的集合中更新您的患者文档。如果这确实是错误,请考虑使用更新它

    snap.ref.update({
        id: clientId
    });
    

    相反。这将使用您在函数触发器上获得的快照中的现有引用,它还将使用更新而不是合并集,这在语法上更正确。它还有助于防止出现上述潜在错误。

    【讨论】:

      【解决方案2】:

      您正在触发一个名为“患者”的集合中的文档:

      exports.updateclient = functions.firestore
        .document('patients/{clientId}')
      

      但是您正在回写到另一个名为“患者”的集合中的文档:

      const patientRef = database.collection('patient').doc(clientId);
      

      您的意思是写回“患者”集合而不是“患者”吗?

      【讨论】:

        猜你喜欢
        • 2020-11-16
        • 2014-12-26
        • 1970-01-01
        • 2012-09-08
        • 1970-01-01
        • 2020-07-29
        • 2016-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多