【问题标题】:What does this arrayUnion method fail in an https cloud function?这个 arrayUnion 方法在 https 云函数中失败的原因是什么?
【发布时间】:2021-03-11 15:32:27
【问题描述】:

预期行为

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

过去的研究

  • seemslike 这可能是 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"
  }
}

任何提示将不胜感激!

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions babeljs


    【解决方案1】:

    数组联合的正确语法是:

    admin.firestore.FieldValue.arrayUnion(dataToAdd)
    

    您的代码中缺少.arrayUnion

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

    【讨论】:

    • 哇!多么令人瞩目的时刻:?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多