question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firestore Emulator doesn't handle admin.firestore.FieldValue.increment on 9.2.0

See original GitHub issue

[REQUIRED] Step 2: Describe your environment

  • Operating System version: Windows
  • Firebase SDK version: 9.2.0
  • Firebase Product: Emulator
  • Node.js version: 10.15.3
  • NPM version: 6.4.1

[REQUIRED] Step 3: Describe the problem

I had a function that does doc.update({field: admin.firestore.FieldValue.increment(1)}), and a test to expect that field === 1. It was working fine when I was on ~8.0.0, but I tried to update to 9.2.0, this test started to fail.

    Expected: 1
    Received: undefined

I wonder if admin.firestore.FieldValue.increment is still properly handled in the emulator?

Steps to reproduce:

  1. Write a cloud function and a test
  2. Run test with it connected to emulator

Relevant Code:

Running test:

firebase emulators:exec --only firestore \"jest --config jest.config.js\"

Connecting to emulator:

const projectId = Math.random()
  .toString(36)
  .substring(7);
process.env.GCLOUD_PROJECT = projectId;
process.env.FIRESTORE_EMULATOR_HOST = "127.0.0.1:8080";
initializeApp({ projectId });

Code for function:

...

        await db.doc(`users/${USER_ID}`).update({
          // This field is undefined before
          field: admin.firestore.FieldValue.increment(1)
        });

...

Test:

  it("successfullly marks an item as purchased if it's made by the requester and increment counter", async () => {
    const wrapped = testEnv.wrap(functionName);
    const data = {};

    const res = await wrapped(data, {
      auth: { uid: USER_ID }
    });
    await expect(res).resolves;

    const userDoc = await db.doc(`users/${USER_ID}`).get();
    expect(userDoc .data()!["field"]).toBe(1);
  });

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
BennetEcommented, Oct 7, 2022

For anyone who still has the problem and is looking for a solution. I found this answer that helped me:

https://github.com/firebase/firebase-admin-node/issues/1803#issuecomment-1179244087 https://stackoverflow.com/a/73047370/10433038

Do not use this:

import * as admin from "firebase-admin";

admin.firestore.FieldValue

use this:

import { FieldValue } from "firebase-admin/firestore";
1reaction
schmidt-sebastiancommented, Sep 21, 2020

@elvisun This issue is usually caused by using types from different versions of Firestore together. Can you verify that you only have a single version of @google-cloud/firestore (and ideally no version of @firebase/firestore) in your dependency tree?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot get FieldValue.increment working in Firestore ...
I am using firebase-admin 8.3.0 . So I am using @firebase/testing for my mocha tests which execute against the firestore emulator which ...
Read more >
Firebase Admin Node.js SDK Release Notes - Google
The Cloud Firestore API now supports the preferRest setting to force the use of REST transport until an operation requires gRPC. Cloud Messaging....
Read more >
Increment a Firestore document field - Google Cloud
Atomically increment the population of the city by 50. await washingtonRef.UpdateAsync("Regions", FieldValue.Increment(50));. View on GitHub Feedback.
Read more >
firebase - UNPKG
firebase/firebase-firestore-lite.js.map. Version: ... n */\n NODE_CLIENT: false,\n /**\n * @define {boolean} Whether this is the Admin Node.js SDK.
Read more >
Firestore - increment a numeric value [resolved]
The Firebase Request doesn't wait for that value to be returned from the function before it makes its call. I discovered this myself...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found