lurning too koad
lurning too koad

Reputation: 2974

How to access Firestore increment method from a v2 Firebase Cloud Function?

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?

Upvotes: 1

Views: 98

Answers (2)

Greg Fenton
Greg Fenton

Reputation: 2808

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.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599876

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)

Upvotes: 1

Related Questions