Firestore +云功能:如何从另一个文档中读取

Tho*_*her 6 javascript google-cloud-functions google-cloud-firestore

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

这是一个寻找如何做这么简单的事情的宝藏.

  1. 云功能文档似乎建议查看管理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);
             })
    });
Run Code Online (Sandbox Code Playgroud)

这种方法失败了:

admin.firestore.getDocument is not a function
Run Code Online (Sandbox Code Playgroud)

我也试过了.admin.firestore.document,admin.firestore.doc,admin.firestore.collection等等.它们似乎都不是一个功能.

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

PS:他们说文件是你的朋友.这个文件是一个噩梦,遵循将所有线索分散到风的四个方向的原则!

Tho*_*her 10

谢谢你,@ 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);
            });
    });
Run Code Online (Sandbox Code Playgroud)