【发布时间】:2018-05-11 08:34:13
【问题描述】:
我正在尝试编写一个从另一个文档读取的 Google 云函数。 (其他文档=不是触发云功能的文档。)
要弄清楚如何做这么简单的事情有点寻宝。
-
云功能文档似乎建议查看 admin SDK:“您可以通过 DeltaDocumentSnapshot 界面或通过 Admin SDK 进行 Cloud Firestore 更改。”
-
Admin SDK 建议编写以下代码行来获取客户端。但是哦,不,它不会向客户解释。它会让我们在文档中的其他地方大打出手。
var defaultFirestore = admin.firestore();“默认的 Firestore 客户端(如果未提供应用)或与提供的应用关联的 Firestore 客户端。”
https://firebase.google.com/docs/reference/admin/node/admin.firestore
-
该链接解析为一般概述页面,没有直接线索来弄清楚接下来的事情。
https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/
-
深入挖掘,有一个很有前途的类,叫做 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) })
所以,我正在尝试将所有这些信息合并到一个谷歌云函数中,该函数将从另一个文档中读取。
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:他们说文档是你的朋友。这份文件是一场噩梦,遵循将所有线索分散到四个风向的原则!
【问题讨论】:
-
这是另一个错误的开始。 Firebase 有一个带有示例的 GitHub 存储库:github.com/firebase/functions-samples/blob/master/quickstarts/… 这是示例的相关部分:
admin.firestore().collection('messages').add({original: original}).then(writeResult =>我已经针对我的代码进行了调整。它也因错误而失败。 -
据我所知,您的第一个 sn-p 应该是
admin.firestore().doc('ruleSets/1234').get().then(...或admin.firestore().collection('ruleSets').doc('1234').get().then(... -
非常感谢!是的,它现在终于可以工作了!
标签: javascript google-cloud-functions google-cloud-firestore