在我的 firebase 云功能中,我已将 firebase-admin 升级到版本 11.0.0(从 9.12.0),并将引擎从 12 升级到 16。之后,当我在模拟器中运行代码时,admin.firestore.FieldValue 未定义。
当我部署代码时,它可以正常工作。
我喜欢在模拟器中测试。我可以做些什么来使其在模拟器中工作吗?
我的代码:
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),
});
我的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
}
而不是使用
admin.firestore.FieldValue
您应该通过以下方式之一导入
FieldValue
:
import { FieldValue } from 'firebase-admin/firestore'
或
const { FieldValue } = require ("firebase-admin/firestore");
然后直接使用
FieldValue
。
要修复您的示例:
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),
});
const { FieldValue } = require("firebase-admin/firestore")
这对我有用