提问者:小点点

在Node.js中将FirebasFiresore. DocumentSnapshot转换为列表/地图


如何将FirebasFiresore. DocumentSnapshot转换为列表/地图以供事后解析?

每个文档中字段的数量各不相同,因此无法手动完成。

文档中没有任何有用的内容:

exports.userDetailsForm = functions.firestore.
document('responseClientDetails/{details}').onCreate((snap, context) => {

  const newValue = snap.data();
  const caseReference = snap.id;



  return Promise
});

共1个答案

匿名用户

如您参考的文档中所述,DocumentSnapshot将返回“包含文档中所有字段的对象”。

如果要将此对象转换为地图,可以使用SO答案中描述的一些技术。例如:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
        for (let [key, value] of Object.entries(doc.data())) {
          console.log(`${key}: ${value}`);
        }
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});