【问题标题】:Property 'then' does not exist on type 'CloudFunction<Change<DocumentSnapshot>>'类型“CloudFunction<Change<DocumentSnapshot>>”上不存在属性“then”
【发布时间】:2020-01-11 00:00:21
【问题描述】:

错误:在我的 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);
});

}

【问题讨论】:

  • 能否提供您的createObjectDocument() 方法。
  • 我添加了功能。

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


【解决方案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() 函数中完成

【讨论】:

  • db.collection("casesToExport").doc(caseNumber).update({key : value }) 由于某种原因从未被调用
  • 可能是因为您在那里拥有的关键价值? console.log(${key}: ${value}); 的控制台日志返回什么?
  • 所有字段格式与此相同 Client_Mobile_Tel: 07783602211
  • 所以你确定你的 if 语句是正确的还是你用 console.log() 测试过?
  • 我设法调用了该函数,但它只创建了一个新的字段键:值。好像不识别键值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2019-01-21
  • 2021-05-31
  • 2020-12-27
  • 2021-05-17
相关资源
最近更新 更多