Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

admin.firestore is not a function when trying to use google cloud functions with node.js

This is the header of the node.js index.js file:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();

This is the node.js function to listen to firestore changes:

exports.myFoo = functions.firestore
  .document('foo/{bar}')
  .onWrite(event => {
    // do stuff
}

This is in the package.json file:

  "dependencies": {
    "firebase-admin": "^5-.4.2",
    "firebase-functions": "^0.7.1",
    "firestore": "^1.1.6"
  },

When I try to do a "firebase deploy" command this is the error I am getting:

Error: Error occurred while parsing your function triggers.
TypeError: admin.firestore is not a function

askFirebase

like image 571
ninj4 n00b Avatar asked Oct 11 '17 03:10

ninj4 n00b


People also ask

How do I use firestore in cloud function?

Cloud Firestore function triggersThe Cloud Functions for Firebase SDK exports a functions. firestore object that allows you to create handlers tied to specific Cloud Firestore events. Triggered when a document is written to for the first time. Triggered when a document already exists and has any value changed.


2 Answers

I was able to reproduce the error and brute-force a solution. I don't know much about npm and can't offer a complete explanation of why this solution worked.

My original package.json contained:

  "dependencies": {
    ...
    "firebase-admin": "^4.2.1",
    "firebase-functions": "^0.7.1",
    ...
  },

As recommended in the documentation, I ran these two commands in the functions folder:

npm install -g firebase-tools
npm install firebase-functions@latest --save

I also tried:

npm install --save firebase-admin
npm upgrade

I repeatedly received these error messages:

+-- UNMET PEER DEPENDENCY [email protected]
npm WARN [email protected] requires a peer of firebase-admin@~5.4.2 but none was installed.

I figured firebase-admin needed to be updated but couldn't make it happen. So I edited the dependencies file to delete this line:

"firebase-admin": "^4.2.1"

then ran npm install --save firebase-admin again. With that, the package.json contained version "firebase-admin": "^5.4.2" and var db = admin.firestore(); compiled without error.

like image 156
Bob Snyder Avatar answered Sep 21 '22 09:09

Bob Snyder


functions.firestore is supposed to be functions.firestore()

like image 37
Martin Avatar answered Sep 21 '22 09:09

Martin