Cloud Functions和Firestore不在Firebase中扎根 [英] Cloud Functions and Firestore does not go up the root in Firebase
问题描述
// 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");
});
要让Firestore与Cloud Functions结合使用,我遇到了很大的困难.当然,它的工作方式有所不同.
I am having great difficulty in getting the root for Firestore working with Cloud Functions. It works differently of course.
在尝试将onUpdate进一步向下触发后,我尝试在指向根的路径上获取一个值.
I am try to get a value up the path towards the root after an onUpdate has been fired further down.
.parent不起作用 functions.database.ref当然不起作用,因为这是实时数据库 并且不能使用 firebase.firestore()在节点中也不起作用 并且event.data.ref.firestore.doc以未定义的形式返回.
.parent does not work functions.database.ref of course does not work as that's the realtime database and cannot use firebase.firestore() is also not working in node and event.data.ref.firestore.doc comes back as undefined.
我确信已经经历了所有选择.
I am sure have gone through every option.
希望您能提供帮助.
哇
推荐答案
根据文档,您可以通过firestore
访问集合,如下所示:
According to the documentation, you can access collections via firestore
, like this:
exports.giveCard = functions.firestore
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}')
.onWrite((event) => {
// other code
const ref = event.data.ref.firestore.doc('your/path/here');
return ref.set({foo: 'bar'}).then(res => {
console.log('Document written');
});
});
您可以使用firestore
为您要访问的数据库的任何部分建立路径.您也可以像这样使用event.data.ref.parent
:
You can use firestore
to build a path to whatever part of the database you're seeking to access. You can also use event.data.ref.parent
, like so:
exports.giveCard = functions.firestore
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}')
.onWrite((event) => {
// other code
const parentref = event.data.ref.parent;
const grandparentref = parentref.parent; // gets to cardsId
return grandparentref.set({foo: 'bar'}).then(res => {
console.log('Document written');
});
});
这篇关于Cloud Functions和Firestore不在Firebase中扎根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!