【问题标题】:Document does not have the fields uploaded (Firestore)文档没有上传字段(Firestore)
【发布时间】:2020-01-09 04:51:25
【问题描述】:

我一直在尝试更新 firebase 文档中的特定字段,但由于某种原因,即使触发了云功能并对文档执行上传,它也不会上传,字段的值永远不会上传。

每当“客户详细信息”中的任何字段更新时,触发器功能就会起作用,然后它会获取新的字段值。 然后在上传的文档上创建一个引用,以便访问要迭代的数组。

然后将文档和文档 ID 传递给一个函数,该函数遍历每个键:更新文档中的值对,然后使用不同文档中的相同键更新字段。

一切正常,但字段值永远不会更新。


exports.rebuildFormTriggerClientDetails = functions.firestore.
document('clientDetails/{details}').onUpdate((change)  => {

  const afterDoc = change.after.data();
  const documentId = change.after.id

  if (afterDoc)

  {

    let docUsers = db.collection("clientDetails").doc(documentId);

    let caseArray: Array<string>;
    caseArray = afterDoc.CaseRefArray;

    for (var valCase of caseArray) {
      console.log(valCase); 
      console.log(documentId);

        createObjectDocument(docUsers,valCase);

    }

  }

  return Promise
});


function createObjectDocument(document: any, caseNumber: String)

{
  document.get().then(function(doc: any) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
        for (let [key, value] of Object.entries(doc.data())) {
          console.log(`${key}: ${value}`);
         if (key != "CaseRefArray")
         {

          db.collection("casesToExport").doc(caseNumber).update({key : value });
         }
        }
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error: any) {
    console.log("Error getting document:", error);
});


  [1]: https://i.stack.imgur.com/duhkq.png

【问题讨论】:

  • 您忽略了 update() 返回的承诺。您的 main 函数需要返回一个仅在所有其他异步工作完成后才解析的 Promise。 return Promise 不会那样做。

标签: typescript firebase google-cloud-firestore google-cloud-functions document


【解决方案1】:

尝试解决您的云函数而不是返回一个承诺。

exports.rebuildFormTriggerClientDetails = functions.firestore.
document('clientDetails/{details}').onUpdate((change)  => {

  const afterDoc = change.after.data();
  const documentId = change.after.id

  if (afterDoc)

  {

    let docUsers = db.collection("clientDetails").doc(documentId);

    let caseArray: Array<string>;
    caseArray = afterDoc.CaseRefArray;

    for (var valCase of caseArray) {
      console.log(valCase); 
      console.log(documentId);

        createObjectDocument(docUsers,valCase);
    }
  }
}).then(() => {
   console.log("Successfully updated document!");
}).catch((error) => {
   console.error("Error updating document: ", error);
});

【讨论】:

  • 不行,可能和上传功能有关。
猜你喜欢
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2020-11-30
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
相关资源
最近更新 更多