4

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

}

Jørgen Rasmussen
  • 1,143
  • 14
  • 31

2 Answers2

3

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

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

Same for TimeStamp

Source

0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
1

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),
});
Leonardo Rignanese
  • 865
  • 11
  • 22