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
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.
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.
functions.firestore is supposed to be functions.firestore()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With