Admin.firestore is not a function when trying to use Google cloud functions using 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 for firestore changes:

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

This is the package.json file:

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

When I try to execute the "firebase deploy" command, this is the error I get:

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

askFirebase

+11
firebase google-cloud-firestore google-cloud-functions
source share
4 answers

I was able to reproduce the error and brute force of the solution. I don’t know much about npm and cannot give a full explanation of why this solution worked.

My original package.json contains:

  "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 have repeatedly received these error messages:

 +-- UNMET PEER DEPENDENCY firebase-admin@4.2.1 npm WARN firebase-functions@0.7.1 requires a peer of firebase-admin@ ~5.4.2 but none was installed. 

I decided that firebase-admin needed to be updated, but could not. So I edited the dependency file to remove this line:

 "firebase-admin": "^4.2.1" 

then ran npm install --save firebase-admin again. At the same time, package.json contained the version of "firebase-admin": "^5.4.2" and var db = admin.firestore(); compiled without errors.

+13
source share

functions.firestore must be functions.firestore()

+1
source share

Try this

 const getReceiverDataPromise = admin.firestore().doc('users/' + receiverUID).get(); const getSenderDataPromise = admin.firestore().doc('users/' + senderUID).get(); return Promise.all([getReceiverDataPromise, getSenderDataPromise]).then(results => { const receiver = results[0].data(); console.log("receiver: ", receiver); const sender = results[1].data(); console.log("sender: ", sender); }); 
0
source share

To solve in file.js file (async method):

 const getFirestore = () => admin.firestore() 

Example:

  getFirestore() .collection(`mailchimp-users`) .doc(uid) .set(profile) .then(() => res.redirect(MAILCHIMP_AUTH_SUCCESS_URL)) .catch((error) => res.json({error})) 
-one
source share

All Articles