1

I am developing a Vue application with Firebase.

My firestore structure Photo

I want to update all field of Id: 5

My code

import * as fb from '../firebase';
const DOC_ID = "xyz";

fb.db.collection("orders").doc(DOC_ID).update({
    regions: fb.db.FieldValue.arrayUnion({ Id: 5, Name: "newName", Mail: "new@email.com", ContactNo: 
"100000000" })
});

But I am getting error:

TypeError: Cannot read property 'arrayUnion' of undefined

I've tried this solutions but nothing work for me

How to update an "array of objects" with Firestore?

https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array

1 Answers1

2

The error is suggesting that fb.db.FieldValue is undefined. That's why you're getting that TypeError.

In the official docs, the arrayUnion function can be accessed by this path: firebase.firestore.FieldValue.arrayUnion

However, you're importing firebase from a local file ../firebase.

We need to know the content of that file, in order to understand why your firebase.db.FieldValue is undefined. Maybe you're not exporting it correctly in the ../firebase file.

It's likely that your ..firebase file is exporting the result of the firebase.firestore(). However, the FieldValue property is not accessed from this object.

Example:

firebase.firestore.FieldValue // This is correct
firebase.firestore().fieldvalue // This is incorrect

I think your fb.db is the result of a firebase.firestore(), and not the result of firebase.firestore

sonikro
  • 86
  • 6