"TypeError:functions.firestore.collection不是函数"

Tho*_*hoe 1 firebase google-cloud-functions google-cloud-firestore

浏览Firestore文档,我看到了许多示例,functions.firestore.document但我没有看到任何示例functions.firestore.collection.Firestore语法是

firebase.firestore().collection('...').doc('...')
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息

firebase.firestore().document('...')
Run Code Online (Sandbox Code Playgroud)

然而在使用此代码的云函数中:

exports.myFunction = functions.firestore.collection('...').doc('...').onUpdate(event => {
Run Code Online (Sandbox Code Playgroud)

在部署时我收到一条错误消息:

TypeError: functions.firestore.collection is not a function
Run Code Online (Sandbox Code Playgroud)

当我将代码更改为

exports.getWatsonTokenFirestore = functions.firestore.document('...').onUpdate(event => {
Run Code Online (Sandbox Code Playgroud)

我在部署时没有收到错误消息.

为什么云功能似乎具有与云Firestore不同的数据结构?

这是我的完整云功能.我的收藏是User_Login_Event,我的文件是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"
            });
Run Code Online (Sandbox Code Playgroud)

该函数部署时没有错误但是当它执行时我收到此错误消息:

TypeError: firebase.firestore is not a function
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为firebase.firestore不在我的云功能中.它在我的Angular前端代码中的各个地方都没有问题.这个错误信息指的是什么?我试过换线

admin.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');
Run Code Online (Sandbox Code Playgroud)

firebase.firestore().collection('IBM_Watson_Token').document('Token_Value').update('token');
Run Code Online (Sandbox Code Playgroud)

console.log("getWatsonTokenFS response");
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的错误信息.

Jas*_*man 7

是.你应该把它格式化为......

exports.getWatsonTokenFirestore = functions.firestore.document('myCollection/{documentId}').onUpdate(event => {
// code
});
Run Code Online (Sandbox Code Playgroud)

collection并且doc是其中的方法firebase.firestore.要通过它访问它们functions.firestore,您必须使用document.

您可以查看Cloud Firestore的完整列表以及适用于Firebase的最新SDK for Cloud Functions

更新

我一直在研究你的代码.我添加了所有的依赖项和初始化,我假设你的代码中有.我无法看到您在IBM Watson请求中使用Firestore中的任何数据,我无法看到您如何将任何返回的数据写回Firestore.由于我不熟悉您的request方法,我已经对它进行了评论,为您提供了对Firestore进行更新的实际示例,并将其写回来.我还编辑了一些代码以使其更具可读性并更改了Cloud Functions代码以反映今天发布的v1.0.0(我已经测试了一段时间):)

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