Firebase &引用;TypeError:functions.firestore.collection不是函数;

Firebase &引用;TypeError:functions.firestore.collection不是函数;,firebase,google-cloud-functions,google-cloud-firestore,Firebase,Google Cloud Functions,Google Cloud Firestore,通过查看Firestore文档,我看到了许多函数.Firestore.document的示例,但没有看到函数.Firestore.collection的示例。Firestore语法是 firebase.firestore().collection('...').doc('...') 我收到一条错误消息 firebase.firestore().document('...') 但在云计算中,使用以下代码: exports.myFunction = functions.firestore.col

通过查看Firestore文档,我看到了许多
函数.Firestore.document
的示例,但没有看到
函数.Firestore.collection
的示例。Firestore语法是

firebase.firestore().collection('...').doc('...')
我收到一条错误消息

firebase.firestore().document('...')
但在云计算中,使用以下代码:

exports.myFunction = functions.firestore.collection('...').doc('...').onUpdate(event => {
部署时,我收到一条错误消息:

TypeError: functions.firestore.collection is not a function
TypeError: firebase.firestore is not a function
当我将代码更改为

exports.getWatsonTokenFirestore = functions.firestore.document('...').onUpdate(event => {
部署时我没有收到错误消息

为什么云功能看起来与云Firestore的数据结构不同

这是我的全部云功能。我的收藏是
User\u Login\u Event
,我的文档是
Toggle\u 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"
            });
函数部署时没有错误,但在执行时,我收到以下错误消息:

TypeError: functions.firestore.collection is not a function
TypeError: firebase.firestore is not a function
我很困惑,因为
firebase.firestore
不在我的云功能中。这是在我的角度前端代码在不同的地方,没有问题。此错误消息指的是什么?我试着换线

admin.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');

以及

console.log("getWatsonTokenFS response");

但我收到了相同的错误消息。

云函数是根据实时数据库中Firebase示例中发生的事件触发的,即身份验证

云firestore是根据firestore中发生的事件触发的,firestore使用文档和集合的概念

如下文所述:


当文档发生更改时,将使用cloud firestore触发器。

云函数是根据Firebase中发生的事件触发的,例如实时数据库中的身份验证

云firestore是根据firestore中发生的事件触发的,firestore使用文档和集合的概念

如下文所述:


当文档发生更改时,将使用cloud firestore触发器。

是。您应该将其格式化为

exports.getWatsonTokenFirestore = functions.firestore.document('myCollection/{documentId}').onUpdate(event => {
// code
});
collection
doc
firebase.firestore
中的方法。要通过
函数.firestore
访问它们,必须使用
文档

您可以看到的类的完整列表和的最新SDK

使现代化 我一直在写你的代码。我已经添加了所有依赖项和初始化,我假设您在代码中已经添加了这些依赖项和初始化。在IBM Watson请求中,我看不到您在哪里使用Firestore中的任何数据,也看不到您如何将任何返回的数据写回Firestore。由于我不熟悉您的
请求
方法,我已经对其进行了注释,以便为您提供Firestore更新的工作示例,并将其写回。我还编辑了一些代码以使其更具可读性,并更改了云函数代码以反映今天发布的v1.0.0(我已经测试了一段时间):)


对。您应该将其格式化为

exports.getWatsonTokenFirestore = functions.firestore.document('myCollection/{documentId}').onUpdate(event => {
// code
});
collection
doc
firebase.firestore
中的方法。要通过
函数.firestore
访问它们,必须使用
文档

您可以看到的类的完整列表和的最新SDK

使现代化 我一直在写你的代码。我已经添加了所有依赖项和初始化,我假设您在代码中已经添加了这些依赖项和初始化。在IBM Watson请求中,我看不到您在哪里使用Firestore中的任何数据,也看不到您如何将任何返回的数据写回Firestore。由于我不熟悉您的
请求
方法,我已经对其进行了注释,以便为您提供Firestore更新的工作示例,并将其写回。我还编辑了一些代码以使其更具可读性,并更改了云函数代码以反映今天发布的v1.0.0(我已经测试了一段时间):)


现在Firebase已将
Firebase admin
更新为5.12.0,将
Firebase函数
更新为1.0.1,我的测试功能正在运行。Jason Berryman编写的函数除了两行之外都是正确的。杰森写道:

.onUpdate((snap, context) => {
return firestore.doc(`IBM_Watson_Token/Token_Value`).update('token')
那应该是

.onUpdate((change, context) => {
其次,杰森写道:

.onUpdate((snap, context) => {
return firestore.doc(`IBM_Watson_Token/Token_Value`).update('token')
更正的行为:

return firestore.collection('IBM_Watson_Token').doc('Token_Value').update({
    token: 'newToken'
  })
我对Jason的代码做了两处修改。首先,我更改了位置语法;下面将对此进行详细介绍。其次,
update()
需要一个对象作为参数

为了显示位置的语法,我编写了一个简单的云函数,当Cloud Firestore中某个位置的值发生更改时触发,然后将新值写入Cloud Firestore中的另一个位置。我删除了行
const firestore=admin.firestore()要使代码更清晰,请执行以下操作:

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

admin.initializeApp();

exports.testFunction = functions.firestore.document('triggerCollection/{documentID}').onUpdate((change, context) => {

  return admin.firestore().collection('writeCollection').doc('documentID').update({
    token: 'newValue'
  })
  .then(response => {
    return Promise.resolve();
  })
  .catch(err => {
    return Promise.reject(err);
  });
});
让我们比较一下CloudFireStore中位置的三种语法。首先,在浏览器中,我使用以下语法:

firebase.firestore().collection('myCollection').doc('documentID')
functions.firestore.document('myCollection/{documentID}')
admin.firestore().collection('myCollection').doc('documentID')
接下来,在云函数触发器中,我使用以下语法:

firebase.firestore().collection('myCollection').doc('documentID')
functions.firestore.document('myCollection/{documentID}')
admin.firestore().collection('myCollection').doc('documentID')
第三,在云函数return中,我使用以下语法:

firebase.firestore().collection('myCollection').doc('documentID')
functions.firestore.document('myCollection/{documentID}')
admin.firestore().collection('myCollection').doc('documentID')
第一行和最后一行是相同的,除了在浏览器中使用
Firebase
调用Firebase外,在服务器上使用
Firebase admin
节点包调用Firebase时,此处别名为
admin

中间线是不同的。它使用
Firebase函数
节点包调用Firebase,这里别名为
函数


换句话说,Firebase是使用不同的库调用的,这取决于您是从浏览器还是从服务器调用(例如,云函数),无论是在云函数中调用触发器还是返回。

现在Firebase已将
Firebase admin
更新为5.12.0,将
Firebase函数
更新为1.0.1,我的测试函数正在工作。Jason Berryman编写的函数除了两行之外都是正确的。杰森写道:

.onUpdate((snap, context) => {
return firestore.doc(`IBM_Watson_Token/Token_Value`).update('token')
那应该是

.onUpdate((change, context) => {
其次,杰森写道:

.onUpdate((snap, context) => {
return firestore.doc(`IBM_Watson_Token/Token_Value`).update('token')
更正的行为:

return firestore.collection('IBM_Watson_Token').doc('Token_Value').update({
    token: 'newToken'
  })
我对Jason的代码做了两处修改。首先,我更改了位置语法;下面将对此进行详细介绍。其次,
update()
需要一个对象作为参数

为了显示位置的语法,我编写了一个简单的云函数,当某个位置上有一个值时会触发该函数