0

Expected Behavior

When calling the admin.firestore.FieldValue method in an https.onCall cloud function, the target document in our Cloud Firestore database is updated with the new data.

Observed Behavior

Each call yields this error:

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) 

Code Sample

This is the Cloud Function I've written and deployed through 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'
  }
)

Past Research

  • It seems like this might be a problem with Babel and ES5/6 transpiling.
  • Interestingly, my coding partner doesn't seem to have this issue with using the exact same syntax, and we are building using the exact same .babelrc setup.
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ],
    "@babel/preset-react"
  ]
}

Tech Stack

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

Any tips would be greatly appreciated!

Davis Jones
  • 1,504
  • 3
  • 17
  • 25

1 Answers1

3

The correct syntax for an array union is:

admin.firestore.FieldValue.arrayUnion(dataToAdd)

You're missing the .arrayUnion in your code.

Also see the Firebase documentation on adding elements to an array.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807