Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function

Viewed 47

Im using NodeJs within google cloud functions and I want to increase the value of one parameter in one document

I initiate Firestore:

const Firestore = require('@google-cloud/firestore');
const PROJECTID = 'XXXX';     
const firestore = new Firestore({
  projectId: PROJECTID,
  timestampsInSnapshots: true    
});

And my .get() and .set() functions work correctly. But when I try to update a value incrementing it, I get errors with FieldValue.increment

I've tried:

await snapshot.ref.update({ parameter: FieldValue.increment(1) });

adding:

const FieldValue = require('firebase-admin').firestore.FieldValue;

await snapshot.ref.update({ parameter: FieldValue.increment(1) });

as explained in other sites, and:

documentRef.update(
  'parameter', Firestore.FieldValue.increment(1)
)

as explained in https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/fieldvalue

const admin = require('firebase-admin');
increment = admin.firestore.FieldValue.increment(1);
await snapshotRef.update({ parameter: increment });

But none work.

Error:

TypeError: admin.firestore.FieldValue.increment is not a function

or

TypeError: FieldValue.increment is not a function

  "dependencies": {
    "firebase-admin": "^6.5.1",
    "nodemailer": "^6.6.1"
  }
2 Answers

The increment() was added to Firestore SDK in April 2019 as mentioned in the release notes but Firebase Admin 6.5.1 was released in January 2019. Upgrading to latest version should resolved this issue.

Just adding the command to update to the latest Firebase Admin version.

npm i firebase-admin@latest

You could also modify the specific package in package.json by changing the version to the latest. See sample below:

"firebase-admin": "latest"

and then run

npm install

Also, for best practice, always remove the node_modules folder and package-lock.json and then run npm install before you deploy your app.

Related