Firestore +云功能:如何从另一个文档中读取 [英] Firestore + cloud functions: How to read from another document

查看:53
本文介绍了Firestore +云功能:如何从另一个文档中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个从另一个文档读取的Google云功能. (其他文档不是触发云功能的文档.)

弄清楚如何做这种简单的事情有点寻宝.

  1. 云功能文档似乎建议您查看admin SDK:您可以通过DeltaDocumentSnapshot界面或Admin SDK进行Cloud Firestore更改.

    https://firebase.google.com/docs/functions/firestore-events

  2. Admin SDK建议编写以下代码行以获取客户端.但是,哦,不,这不会解释客户.它将把我们送往文档中其他地方的野鹅追捕.

    var defaultFirestore = admin.firestore();

    如果未提供任何应用程序,则为默认的Firestore客户端,或者与提供的应用程序关联的Firestore客户端."

    https://firebase.google.com/docs/reference/admin/node/admin.firestore

  3. 该链接解析为常规概述页面,没有直接线索找出下一步.

    https://cloud.google.com/nodejs/docs /reference/firestore/0.10.x/

  4. 深入研究,发现了一个很有前途的类,称为FireStoreClient.它有一个"getDocument"方法,看起来很有希望.该参数似乎很复杂.与其将路径简单地传递给方法,不如希望将整个文档/集合作为参数.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument

    var formattedName = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]"); client.getDocument({name: formattedName}).then(function(responses) { var response = responses[0]; // doThingsWith(response) })

因此,我正在尝试将所有这些信息组合到将从另一个文档中读取的Google云功能中.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.updateLikeCount4 = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .getDocument('ruleSets/1234')
            .then(function(responses) {
                 var response = responses[0];
                 console.log('Here is the other document: ' + response);
             })
    });

该方法失败:

admin.firestore.getDocument is not a function

我也尝试过. admin.firestore.document,admin.firestore.doc,admin.firestore.collection等.它们似乎都不是函数.

我只想从我的Google云功能中的另一个Firestore文档中读取内容.

PS:他们说文档是您的朋友.该文档是一场噩梦,它遵循将所有线索分散到风的四个方向上的原则!

解决方案

谢谢,@ frank-van-puffelen.

这是可行的解决方案:

exports.updateLikeCount = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .collection('ruleSets')
            .doc(1234)
            .get()
            .then(doc => {
                console.log('Got rule: ' + doc.data().name);
            });
    });

I'm trying to write a Google cloud function that reads from another document. (Other document = not the document that triggered the cloud function.)

It's a bit of a treasure hunt to figure out how to do such a simple thing.

  1. The cloud functions documentation seems to suggest to look at the admin SDK: "You can make Cloud Firestore changes via the DeltaDocumentSnapshot interface or via the Admin SDK."

    https://firebase.google.com/docs/functions/firestore-events

  2. The Admin SDK suggest to write the following line of code to get a client. But oh no, it's not going to explain the client. It's going to send us off to a wild goose chase elsewhere in the documentation.

    var defaultFirestore = admin.firestore();

    "The default Firestore client if no app is provided or the Firestore client associated with the provided app."

    https://firebase.google.com/docs/reference/admin/node/admin.firestore

  3. That link resolves to a general overview page with no direct clue on figuring out the next thing.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/

  4. Digging a big around, there is a promising class called FireStoreClient. It has a 'getDocument' method that seems promising. The parameter seems complicated. Rather than simply passing the path into the method, it seems to want an entire document/collection something as a parameter.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument

    var formattedName = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]"); client.getDocument({name: formattedName}).then(function(responses) { var response = responses[0]; // doThingsWith(response) })

So, I'm trying to combine all of this information into a Google cloud function that will read from another document.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.updateLikeCount4 = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .getDocument('ruleSets/1234')
            .then(function(responses) {
                 var response = responses[0];
                 console.log('Here is the other document: ' + response);
             })
    });

That approach fails with:

admin.firestore.getDocument is not a function

I've also tried. admin.firestore.document, admin.firestore.doc, admin.firestore.collection, and many more. None of them seem to be a function.

All I want is to read from another Firestore document in my Google cloud function.

PS: They said the documentation is your friend. This documentation is a nightmare that follows the principle of scatter all the clues into the four directions of the wind!

解决方案

Thank you, @frank-van-puffelen.

This is the working solution:

exports.updateLikeCount = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .collection('ruleSets')
            .doc(1234)
            .get()
            .then(doc => {
                console.log('Got rule: ' + doc.data().name);
            });
    });

这篇关于Firestore +云功能:如何从另一个文档中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆