0

I have a cloud function in firebase emulator and I am trying to update an array. However when the function runs, I get the following error: TypeError: Cannot read properties of undefined (reading 'arrayRemove'). I'm not sure what is going wrong. My node version is 16.

This is my function code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

const db = admin.firestore();
const storage = admin.storage();

exports.completeEventRegistration = functions.firestore
    .document("<path>")
    .onCreate(async (snap, context) => {

     // <ommitted>

      try {

        await db.runTransaction(async (transaction) => {

          // <omitted>   

          const fieldValue = admin.firestore.FieldValue;

          transaction.update(committeeRef, {"countries": fieldValue.arrayRemove({
            "name": chosenCountry,
            "taken": false,
          })});


        });

      } catch (error) {
        console.log("Error registering: ", error);
        throw new functions.https.HttpsError("internal", "Error registering");
      }

    });

3
  • 1
    It's saying that fieldValue is undefined. Are you sure that admin.firestore.FieldValue exists when you are trying to set fieldValue?
    – mykaf
    May 1 at 13:54
  • @mykaf I just checked with this: functions.logger.log(Object.keys(admin.firestore)). It outputs an empty array. Any idea what might be happening? May 1 at 17:13
  • Sorry, I'm not familiar with firebase.
    – mykaf
    May 1 at 18:02

2 Answers 2

0

You need to declare fieldValue as follows, with require:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const fieldValue = require('firebase-admin').firestore.FieldValue;
3
  • It has been declared above the update operation May 1 at 17:11
  • Not as shown in my answer, see the require. May 1 at 18:26
  • I tried it the way shown in your answer as well. It still gives me the same error: Error registering: TypeError: Cannot read properties of undefined (reading 'arrayRemove') May 2 at 4:33
0

I fixed the issue thanks to this GitHub issue.

Following is the fixed code:

const
   { initializeApp } = require('firebase-admin/app'),
   { getFirestore } = require('firebase-admin/firestore');

const IS_EMULATOR = ((typeof process.env.FUNCTIONS_EMULATOR === 'boolean' && process.env.FUNCTIONS_EMULATOR) || process.env.FUNCTIONS_EMULATOR === 'true');

initializeApp();
let firestore = getFirestore();

if (IS_EMULATOR) {
   firestore.settings({
     host: 'localhost',
     port: '<firestore emulator port>',
     ssl: false
   })
}
transaction.update(committeeRef, {"countries": FieldValue.arrayRemove({
  "name": chosenCountry,
  "taken": false
})});

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.