首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在https云函数中,这个arrayUnion方法失败了什么?

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

Stack Overflow用户
提问于 2021-03-11 15:32:27
回答 1查看 77关注 0票数 0

预期行为

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

观察行为

每次调用都会产生以下错误:

代码语言:javascript
复制
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编写和部署的云函数:

代码语言:javascript
复制
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设置进行构建。
代码语言:javascript
复制
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ],
    "@babel/preset-react"
  ]
}

技术栈

代码语言:javascript
复制
{
  "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"
  }
}

任何提示都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-11 15:57:02

数组联合的正确语法是:

代码语言:javascript
复制
admin.firestore.FieldValue.arrayUnion(dataToAdd)

您在代码中缺少了.arrayUnion

还请参阅向数组中添加元素上的Firebase文档。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66585605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档