类型'CloudFunction <Change <DocumentSnapshot >>'上不存在属性'then'

时间:2019-09-09 08:24:34

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

错误:我的Firebase云函数收到的类型'CloudFunction>'不存在属性'then',有人知道如何解决该问题吗?



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);
    //var userMap: {[key: string]: any} = {};
    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: any) => {
  console.error("Error updating document: ", error);
});

此处的createObjectDocument函数。此功能可能是错误的原因吗?


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 {
        console.log("No such document!");
    }
}).catch(function(error: any) {
    console.log("Error getting document:", error);
});

}

1 个答案:

答案 0 :(得分:1)

像这样更改代码:

exports.rebuildFormTriggerClientDetails = functions.firestore.
document('clientDetails/{details}').onUpdate((change)  => {
  //const before = change.before;  // DataSnapshot before the change
  const afterDoc = change.after.data();
  const documentId = change.after.id

  if (afterDoc)

  {
    let docUsers = db.collection("clientDetails").doc(documentId);
    //var userMap: {[key: string]: any} = {};
    let caseArray: Array<string>;
    caseArray = afterDoc.CaseRefArray;

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

        createObjectDocument(docUsers,valCase);
    }
  }

});



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 })
            .then(() => {
                console.log("Successfully updated document!");
            }).catch((error: any) => {
                console.error("Error updating document: ", error);
            });
         }
        }
    } else {
        console.log("No such document!");
    }
}).catch(function(error: any) {
    console.log("Error getting document:", error);
});

}

您的.then()调用必须是在数据库中更新文档时完成的,而这是在createObjectDocument()函数中完成的