Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
a month ago

The error message states that the property 'arrayRemove' cannot be read because it is undefined.

While updating an array in a cloud function running on Firebase emulator, I encountered an error. The error is as follows: TypeError: Cannot read properties of undefined (reading 'arrayRemove'). I'm not sure what went wrong, and my node version is 16. Here's the code of the function:

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");
      }

    });

Can you help me understand what the problem could be? Thanks!

Answers(2)
avatar
Vogi
a month ago
Verified Answer
The error message you're seeing is a common type error in JavaScript that occurs when a property is read or a function is called on an undefined variable. In your case, it seems that the arrayRemove method is being called on an undefined variable. To fix this error, you should make sure that the variable you're calling the arrayRemove method on is defined. You can do this by checking if the variable is defined before calling the method. Here's an example:
myArray.arrayRemove(item);
}
In your code, you can try checking if the fieldValue variable is defined before calling the arrayRemove method on it. Here's how you can do it:
transaction.update(committeeRef, {"countries": fieldValue.arrayRemove({
    "name": chosenCountry,
    "taken": false,
  })});
}
I hope this helps! Let me know if you have any more questions.

Sources

Resolving TypeError: Cannot Read Property of Undefined in JavaScriptHow to Fix TypeError: Cannot read Property 'push' of Undefined in ...TypeError: Cannot read properties of undefined (reading 'arrayRemove')Cannot read properties of undefined (reading 'path') Firebase ref ...[CF3v2 deploy on fresh project] TypeError: Cannot read properties of ...
avatar
Tolerim
a month ago
The error message TypeError: Cannot read properties of undefined (reading 'arrayRemove') is indicating that the arrayRemove() method is not defined for FieldValue, which is most likely because the version of the Firebase SDK being used does not support it. arrayRemove() is a method that was added in version 7.15.0 of the Firebase SDK for Node.js. Since you mentioned that your Node version is 16, you might want to check the version of the Firebase SDK that you are using. To check the version of Firebase SDK currently being used, run:
You should see something like the following:
└── [email protected]
In the above example, the Firebase SDK version being used is 4.0.0. To use arrayRemove() method, you need to upgrade your version of Firebase SDK to at least version 7.15.0. To do so, run the following command:
This will install the latest versions of the Firebase SDK for Node.js. Once you have upgraded, you should be able to use the arrayRemove() method as expected.
;