【发布时间】:2017-10-23 15:39:57
【问题描述】:
// The Cloud Functions for Firebase SDK to create Cloud Functions and
setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.giveCard = functions.firestore
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}')
.onWrite((event) => {
// Get the field values of what I am working with
const oldGiven = event.data.previous.data()['given'];
const newGiven = event.data.data()['given'];
// Get the cardID to make sure that is there
const cardID = event.params.cardsId;
// An array to go through
const give_profiles = event.data.data()['given_profiles'];
// error cardDatatwo is returned as undefined
const cardDatatwo = newGiven.parent;
// error cardDatathree is returned as undefined
const cardDatathree = event.data.ref.root
// // error cardDatafour cannot read propoerty of undefined
// const cardDatafour = cardDatathree.child('Profiles/{profileId}/cards/{cardsId}')
// error cardDatafive 'The value of cardfive is DocumentReference...
const cardDatafive = event.data.ref.firestore.doc('Profiles/{profileId}/cards/{cardsId}');
// Check that the values have changed
if (newGiven == oldGiven) return;
if (newGiven !== undefined) {
console.log('The old value of given is', oldGiven);
console.log('The new value of given is', newGiven);
console.log('The value of the card is', cardID);
console.log('The value of the cardtwo is', cardDatatwo);
console.log('The value of the cardthree is', cardDatathree);
// console.log('The value of the cardfour is', cardDatafour);
console.log('The value of the cardfive is', cardDatafive);
for (var profile of give_profiles) {
console.log(profile);
};
return;
}
return console.log("No given value");
});
我很难获得使用 Cloud Functions 的 Firestore 的根目录。当然,它的工作方式不同。
在进一步向下触发 onUpdate 之后,我试图在通往根的路径上获取一个值。
.parent 不起作用 functions.database.ref 当然不起作用,因为那是实时数据库 并且不能使用 firebase.firestore() 在节点中也不起作用 并且 event.data.ref.firestore.doc 以未定义的形式返回。
我确信已经经历了所有选择。
希望你能帮忙。
我
【问题讨论】:
-
你能分享一些你的代码来展示你已经走了多远以及你需要在哪里实现
parent?这会在带有DocumentSnapshot的侦听器回调中吗? -
请出示您的代码,并记住您应该将您的 firebase-admin 模块更新到最新版本,以便能够使用其 Firestore API。
标签: firebase google-cloud-functions google-cloud-firestore