【问题标题】:CloudFirestore Triggers :How to get the data of the triggered firestore document in cloud functionsCloud Firestore Triggers:如何在云函数中获取触发的firestore文档的数据
【发布时间】:2018-03-20 09:50:44
【问题描述】:

我写了一个云函数来获取触发的文档数据。但我得到 [object object]

我的代码是:

exports.accept = functions.firestore
.document('deyaPayUsers/{aID}/Split/{aID1}/ReceivedInvitation/{autoIds}')
  .onWrite(event=>{
  const db1 = admin.firestore();
  const MAuth = event.params.aID;
  console.log("mauthid"+MAuth);
  const MAuth1 = event.params.aID1;
  const resautoid = event.params.autoIds;
  console.log("resss"+resautoid);
var document = event.data.data();//not getting data 
    console.log("newvalue is :"+document);it prints [object object]

  });

如何使用 FireStore 触发器中的云函数获取数据。

【问题讨论】:

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


    【解决方案1】:

    当您调用 event.data.data() 时,您会以 JavaScript 对象的形式返回文档数据。显然,[Object object] 是 JavaScript 对象的打印方式。如果您想将 JSON 视为字符串,请使用 JSON.stringify 进行转换。例如

    console.log(JSON.stringify(event.data.data());
    

    为了更容易阅读 JSON,您可能还需要考虑:

    console.log(JSON.stringify(event.data.data(), null, '  ');
    

    有关详细信息,请参阅documentation for JSON.stringify on MDN

    【讨论】:

    • console.log(JSON.stringify(event.data.data(), null, ' '); 在这些行我正在获取文档的数据。但我需要获取特定字段当我尝试做 var document = JSON.stringify(event.data.data(), null, ' '); console.log("document is doc"+document);// 在这里我得到文档数据 var senderauthid = document.SenderID;// undefined 正在获取
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    相关资源
    最近更新 更多