skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

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


  2. It seems like you’re not correctly setting a reference to your document.

    Try const docRef = db.collection("stat").doc("users").

    Instead of using set and giving it merge:true option you can just use update like this:

    docRef.update({
      totalUsers: admin.firestore.FieldValue.increment(1),
    });
    

    This will increment totalUser count by 1 even if there was no field name totalUsers before.

    Login or Signup to reply.
  3. It`s a issue with modularity improvement of firebase-admin. You can I rolled back to firebase-admin v10.3.0 or use modular import import { FieldValue } from 'firebase-admin/firestore';

    import * as functions from "firebase-functions";
    import * as admin from "firebase-admin";
    import { FieldValue } from 'firebase-admin/firestore';
    
    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: FieldValue.increment(1),
        }, {merge: true});
    
        } catch (e) {
            console.log("Something is wrong: ", e);
            return Promise.reject(e);
        }
    });
    

    Or you can fully convert to modular import:

    import type { FieldValue, getFirestore } from 'firebase-admin/firestore';
    import * as functions from "firebase-functions";
    const { initializeApp } = require('firebase-admin/app');
    
    
    const app = initializeApp();
    const db = getFirestore();
    
    export const newUserIncrementStat = 
    functions.firestore.document("users/{uid}").onCreate((snap, context) => 
    {
        const docRef = db.doc("stats/users");
    
        try {
        return docRef.set({
            totalUsers: FieldValue.increment(1),
        }, {merge: true});
    
        } catch (e) {
            console.log("Something is wrong: ", e);
            return Promise.reject(e);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search