Why is admin.firestore.FieldValue undefined when running code in firebase emulator?
Asked Answered
L

3

7

In my firebase cloud functions, I have upgraded firebase-admin to version 11.0.0 (from 9.12.0) and engines from 12 to 16. After that admin.firestore.FieldValue is undefined when I run my code in the emulator.

When I deploy the code it works correctly.

I like to test in the emulator. Is there anything I can do to make this work in the emulator?

My code:

import * as admin from 'firebase-admin';

// Remove user permissions
await firestore.doc(`/${projectsPath}/${projectKey}`).update({
        roUids: admin.firestore.FieldValue.arrayRemove(uidOfUserToDelete),
        rwUids: admin.firestore.FieldValue.arrayRemove(uidOfUserToDelete),
});

My package.json:

{
"name": "functions",
"scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "watch": "tsc --watch",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
},
"main": "lib/src/index.js",
"dependencies": {
    "@sendgrid/mail": "^7.7.0",
    "api2pdf": "^1.1.1",
    "axios": "^0.19.2",
    "firebase-admin": "^11.0.0",
    "firebase-functions": "^3.22.0",
    "firebase-tools": "^11.2.0",
    "googleapis": "^104.0.0",
    "lodash": "^4.17.21",
    "mailchimp-api-v3": "^1.13.1",
    "md5": "^2.2.1",
    "onedrive-api": "^1.0.1",
    "pdf-lib": "^1.17.1",
    "php-serialize": "^3.0.1",
    "source-map-support": "^0.5.21"
},
"engines": {
    "node": "16"
},
"devDependencies": {
    "@types/lodash": "^4.14.182",
    "@types/node": "^16",
    "@types/php-serialize": "^3.0.0",
    "@typescript-eslint/eslint-plugin": "^5.30.4",
    "@typescript-eslint/parser": "^5.30.4",
    "eslint": "^8.19.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.26.0",
    "typescript": "^4.7.4"
},
"private": true

}

Lavella answered 10/7, 2022 at 12:4 Comment(3)
Can you try using import { FieldValue } from "firebase-admin/firestore"; instead?Dihydrostreptomycin
If I try import { FieldValue } from 'firebase-admin/firestore'; I get Unable to resolve path to module 'firebase-admin/firestore'Arawn
Please check the stackoverflow URL #69167659Onehorse
M
9

The solution is to update your import.
This worked for me:

import { FieldValue } from 'firebase-admin/firestore'

Same for TimeStamp

Source

Matthiew answered 20/7, 2022 at 7:12 Comment(0)
T
6

Instead of using

admin.firestore.FieldValue

You should import FieldValue in one of these ways:

import { FieldValue } from 'firebase-admin/firestore'

or

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

And then use directly FieldValue.


To fix your example:

import * as admin from 'firebase-admin';
import { FieldValue } from 'firebase-admin/firestore'

// Remove user permissions
await firestore.doc(`/${projectsPath}/${projectKey}`).update({
        roUids: FieldValue.arrayRemove(uidOfUserToDelete),
        rwUids: FieldValue.arrayRemove(uidOfUserToDelete),
});
Teferi answered 10/8, 2023 at 13:34 Comment(0)
V
0

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

this work for me

Velazquez answered 11/7 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.