skip to Main Content

In Firebase Cloud Functions v1, to increment a Firestore field, one would import admin and access it from FieldValue.

const admin = require("firebase-admin");

admin.firestore.FieldValue.increment(1)

However, in v2, this is no longer the case. And attempting to do this through the firestore instance does not work.

const {getFirestore} = require("firebase-admin/firestore");

getFirestore().FieldValue.increment(1) // this does not work

What is the proper way to access the increment method in v2?

2

Answers


  1. In the modular API, increment is a global, top-level function.

    So import it with:

    const {getFirestore, increment} = require("firebase-admin/firestore");
    

    And then use the function where needed, without any namespace:

    increment(1)
    
    Login or Signup to reply.
  2. As @puf said, the change is to the way that the Admin SDK has evolved in terms of its API exports for its many services including Firestore. This is not an issue of Cloud Functions Gen 1 vs Gen 2. The changes to the Admin SDK and Client SDK in terms of "modular API" have happened roughly in the same timeline that Gen 2 got introduced to Cloud Functions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search