"TypeError:functions.firestore.collection不是函数"
在Firestore文档中,我看到了许多functions.firestore.document
的示例,但是没有看到functions.firestore.collection
的任何示例. Firestore语法为
Looking through the Firestore documentation, I see many examples of functions.firestore.document
but I don't see any examples of functions.firestore.collection
. Firestore syntax is
firebase.firestore().collection('...').doc('...')
我收到一条错误消息
firebase.firestore().document('...')
使用以下代码在Cloud Functions中使用
Yet in Cloud Functions with this code:
exports.myFunction = functions.firestore.collection('...').doc('...').onUpdate(event => {
在部署时,我收到一条错误消息:
on deploy I get an error message:
TypeError: functions.firestore.collection is not a function
当我将代码更改为
exports.getWatsonTokenFirestore = functions.firestore.document('...').onUpdate(event => {
部署时没有收到错误消息.
I don't get an error message on deploy.
为什么Cloud Functions似乎具有与Cloud Firestore不同的数据结构?
Why does Cloud Functions appear to have a different data structure than Cloud Firestore?
这是我完整的云功能.我的收藏集是User_Login_Event
,我的文档是Toggle_Value
:
Here's my full Cloud Function. My collection is User_Login_Event
and my document is Toggle_Value
:
exports.getWatsonTokenFS = functions.firestore.document('User_Login_Event/{Toggle_Value}').onUpdate(event => {
var username = 'TDK',
password = 'swordfish',
url = 'https://' + username + ':' + password + '@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api';
request({url: url}, function (error, response, body) {
admin.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');
});
return 0; // prevents an error message "Function returned undefined, expected Promise or value"
});
该函数的部署没有错误,但是在执行时,我收到以下错误消息:
The function deploys without error but when it executes I get this error message:
TypeError: firebase.firestore is not a function
我很困惑,因为firebase.firestore
不在我的Cloud Function中.毫无问题,它在我的Angular前端代码中.此错误消息指的是什么?我试图换行
I'm confused as firebase.firestore
isn't in my Cloud Function. It's in my Angular front-end code in various places, without a problem. What is this error message referring to? I tried changing the line
admin.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');
到
firebase.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');
和
console.log("getWatsonTokenFS response");
但是我得到了相同的错误消息.
but I got the same error message.
是.您应该将其格式化为...
Yes. You should format it as...
exports.getWatsonTokenFirestore = functions.firestore.document('myCollection/{documentId}').onUpdate(event => {
// code
});
collection
和doc
是firebase.firestore
中的方法.要通过functions.firestore
访问它们,必须使用document
.
collection
and doc
are methods within firebase.firestore
. To access them via functions.firestore
, you must use document
.
您可以看到 Cloud Firestore 以及用于 Firebase的云功能
我一直在研究您的代码.我已经添加了所有依赖项和初始化,我假定您在代码中拥有这些依赖项和初始化.我在IBM Watson请求中看不到您正在使用Firestore中的任何数据,也看不到如何将返回的任何数据写回到Firestore中.由于我不熟悉您的request
方法,因此我对其进行了注释,以便为您提供Firestore更新并回写内容的有效示例.我还编辑了一些代码以使其更具可读性,并更改了Cloud Functions代码以反映今天发布的v1.0.0(我已经对其进行了一段时间的测试):)
I've been working on your code. I've added in all of the dependencies and initialization, which I assume that you have in your code. I can't see where you're using any data from Firestore in your IBM Watson request and I can't see how you're writing any of the returned data back to Firestore. As I'm not familiar with your request
method, I've commented it out, to give you what should be a working example of an update to Firestore and writes something back. I also edited some of your code to make it more readable and changed the Cloud Functions code to reflect v1.0.0, released today (I've been testing it for a while) :)
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp();
const firestore = admin.firestore();
exports.getWatsonTokenFS = functions.firestore
.document('User_Login_Event/{Toggle_Value}')
.onUpdate((snap, context) => {
let username = 'TDK';
let password = 'swordfish';
let url = `https://${username}:${password}@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api`;
// request({url}, function (error, response, body) {
// firestore.doc(`${IBM_Watson_Token}/${Token_Value}`).update('token');
// });
return firestore.doc(`IBM_Watson_Token/Token_Value`).update('token')
.then(response => {
return Promise.resolve();
})
.catch(err => {
return Promise.reject(err);
});
});