CloudFirestore触发器:如何在云功能中获取触发的firestore文档的数据 [英] CloudFirestore Triggers :How to get the data of the triggered firestore document in cloud functions
问题描述
我写了一个云函数来获取触发的文档数据。但我得到[对象]
I wrote a cloud functions to get the triggered document data . But I am getting [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触发器中的云函数获取数据。
How to get the data using cloud functions from FireStore triggers.
推荐答案
当您调用 event.data.data()
时,您将文档数据作为JavaScript对象返回。显然, [Object object]
是一个JavaScript对象的打印方式。如果要将JSON视为字符串,请使用 JSON.stringify
进行转换。例如
When you call event.data.data()
you get the document data back as a JavaScript object. Apparently that [Object object]
is how a JavaScript object is printed. If you want to see the JSON as a string, use JSON.stringify
to convert it. E.g.
console.log(JSON.stringify(event.data.data());
为了更容易阅读JSON,您可能还需要考虑:
To make it easier to read the JSON, you might also want to consider:
console.log(JSON.stringify(event.data.data(), null, ' ');
有关详细信息,请参阅 JSON.stringify 的文件rel =nofollow noreferrer。
For more information see the documentation for JSON.stringify
on MDN.
这篇关于CloudFirestore触发器:如何在云功能中获取触发的firestore文档的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!