【问题标题】:Cloud Functions and Firestore does not go up the root in FirebaseCloud Functions 和 Firestore 不会在 Firebase 中根深蒂固
【发布时间】: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


【解决方案1】:

根据the documentation,你可以通过firestore访问收藏,像这样:

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,像这样:

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

});

【讨论】:

  • TY 回复。对不起,我也应该把那个放进去。我以前试过。仍然未定义。我确信现在我已经尝试了所有可能的排列。我已更改代码以添加该错误消息。
  • 它确实有效,因为我自己尝试过。但是,由于大括号,您的参考 const cardDatafive = event.data.ref.firestore.doc('Profiles/{profileId}/cards/{cardsId}'); 不起作用。您不能在尝试写入或读取数据的特定路径中使用通配符。
  • 我还更新了我的答案以显示一个使用 event.data.ref.parent 的示例,它可以访问更高的写入发生路径。
  • 当然。我的错。您正在使用 set ,我应该使用 .get 来获取数据。现在可以工作了。我在访问文档中的特定字段时感到困惑,应该考虑文档中的整个数据。文档、字段、地图。一切对我来说都很新鲜。感谢您的耐心等待。顺便一提。喜欢 Youtube 视频,感谢您回答我的愚蠢问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 2020-02-01
相关资源
最近更新 更多