Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: functions.firestore.collection is not a function"

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('...')

I get an error message with

firebase.firestore().document('...')

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

When I change the code to

exports.getWatsonTokenFirestore = functions.firestore.document('...').onUpdate(event => {

I don't get an error message on deploy.

Why does Cloud Functions appear to have a different data structure than Cloud Firestore?

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

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');

to

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

and to

console.log("getWatsonTokenFS response");

but I got the same error message.

like image 747
Thomas David Kehoe Avatar asked Jul 23 '26 18:07

Thomas David Kehoe


1 Answers

Yes. You should format it as...

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

collection and doc are methods within firebase.firestore. To access them via functions.firestore, you must use document.

You can see a full list of Classes for Cloud Firestore and the latest SDK for Cloud Functions for Firebase

Update

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);
    });
});
like image 102
Jason Berryman Avatar answered Jul 28 '26 06:07

Jason Berryman