为什么这个arrayUnion方法在https云功能中失败?



预期行为

当调用https.onCall云函数中的admin.firestore.FieldValue方法时,cloud Firestore数据库中的目标文档将使用新数据更新。

观察到的行为每次调用都会产生以下错误:

Unhandled error TypeError: Class constructor FieldValue cannot be invoked without 'new'
at /workspace/dist/index.js:2245:38
at Generator.next (<anonymous>)
at asyncGeneratorStep (/workspace/dist/index.js:19:103)
at _next (/workspace/dist/index.js:21:194)
at /workspace/dist/index.js:21:364
at new Promise (<anonymous>)
at /workspace/dist/index.js:21:97
at /workspace/dist/index.js:2255:19
at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)
at process._tickCallback (internal/process/next_tick.js:68:7) 

代码示例

这是我通过Firebase编写并部署的云功能:

exports.recordReferralRegistration = functions.https.onCall(async ({ arguments }) => {
// why is this not working?
// adds the value to a document
.firestore()
.collection("users")
.doc(xyz)
.update({
activityCodes: admin.firestore.FieldValue(dataToAdd),
})
return 'success'
}
)

过去的研究

  • 似乎这可能是Babel和ES5/6编译的问题。
  • 有趣的是,我的编码伙伴似乎没有使用完全相同的语法这个问题,我们正在构建使用完全相同的.babelrc设置。
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "10"
}
}
],
"@babel/preset-react"
]
}

技术堆栈
{
"name": "@squaredeal/functions",
"version": "1.0.0",
"private": true,
"main": "dist/index.js",
"scripts": {
"build": "babel index.js --out-dir dist/",
"deploy": "firebase deploy",
"format": "prettier-eslint",
"format-write": "prettier-eslint --write"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"prettier-eslint --write"
]
},
"dependencies": {
"@babel/preset-react": "^7.12.13",
"@sendgrid/mail": "^7.4.1",
"algoliasearch": "^4.7.0",
"firebase": "^8.2.10",
"firebase-admin": "^9.4.2",
"firebase-functions": "^3.13.1",
"firebase-tools": "^9.6.0",
"googleapis": "^59.0.0",
"moment-timezone": "^0.5.32",
"node-fetch": "^2.6.1",
"nvm": "^0.0.4",
"react-query": "^2.23.1",
"sendgrid": "^5.2.3",
"stripe": "^8.96.0",
"twilio": "^3.49.1"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"prettier-eslint": "^12.0.0"
},
"engines": {
"node": "10"
}
}

任何提示将非常感激!

数组联合的正确语法是:

admin.firestore.FieldValue.arrayUnion(dataToAdd)

你的代码中缺少.arrayUnion

请参阅Firebase文档中关于向数组添加元素的说明。

最新更新