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");
}
});
2
Answers
I fixed the issue thanks to this GitHub issue.
Following is the fixed code:
You need to declare
fieldValue
as follows, withrequire
: