Search code examples
typescriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-tools

Cloud Functions FieldValue increment TypeError in Firestore


I am testing cloud functions in the firebase emulator and getting an error when trying to increment a field in the cloud firestore. Please check my code & error message below. Thanks for any help!

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

const db = admin.firestore();

export const newUserIncrementStat = functions.firestore.document("users/{uid}").onCreate((snap, context) => {
  const docRef = db.doc("stats/users");

  try {
    return docRef.set({
      totalUsers: admin.firestore.FieldValue.increment(1),
    }, {merge: true});

  } catch (e) {
    console.log("Something is wrong: ", e);
    return Promise.reject(e);
  }
});

Error Message from Firebase Logs:

TypeError: Cannot read properties of undefined (reading 'increment')

Dependencies

firebase-admin: "^11.0.0"

firebase-functions: "^3.22.0"

firebase: 11.3.0


Solution

  • as @Min commented the error was with the firebase emulator, deploying the function directly to google cloud works without any error.