这个 arrayUnion 方法在 https 云函数中失败了什么?

What does this arrayUnion method fail in an https cloud function?

预期行为

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 编写和部署的 Cloud Function:

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'
  }
)

过去的研究

{
  "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

另请参阅 adding elements to an array 上的 Firebase 文档。