Cloud Functions and Firestore does not go up the r

2020-08-04 06:02发布

问题:

// 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");
});

I am having great difficulty in getting the root for Firestore working with Cloud Functions. It works differently of course.

I am try to get a value up the path towards the root after an onUpdate has been fired further down.

.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.

Hope you can help.

Wo

回答1:

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');
    });

});

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');
    });

});