CloudFirestore Triggers :How to get the data of th

2020-08-04 09:48发布

问题:

I wrote a cloud functions to get the triggered document data . But I am getting [object object]

My code is :

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]

  });

How to get the data using cloud functions from FireStore triggers.

回答1:

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());

To make it easier to read the JSON, you might also want to consider:

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

For more information see the documentation for JSON.stringify on MDN.