Search code examples
angulartypescriptfirebasegoogle-cloud-firestoreangularfire

Firestore Update Error: Using arrayUnion in Firestore Update Operation


I'm currently facing an issue while trying to update a Firestore document by adding an element to an array field using Firestore's 'arrayUnion' method. I'm using Firestore in my Angular application with AngularFire, and the specific error I'm encountering is:

Type 'FieldValue' is missing the following properties from type 'CarAttachment[]': length, pop, push, concat

The expected type comes from property 'attachments,' which is declared here on type 'Partial'.

Here's the relevant code snippet I'm using for this operation:

addCarAttachment(carId: string, attachment: CarAttachment) {
  return this.firestore.collection<Car>('car').doc(carId).update({
    attachments: FieldValue.arrayUnion(attachment),
  });
}

Additionally, I have defined the following interfaces for 'Car' and 'CarAttachment':

export interface Car {
    id: string;
    attachments: CarAttachment[];
    created_at: Date | Timestamp;
    updated_at?: Date | Timestamp;
}

export interface CarAttachment {
    doc_name: string;
    doc_url: string;
    doc_type: string;
    expiration_time: Date | Timestamp;
    uploaded_at: Date | Timestamp;
}

I'm seeking advice or solutions to help me overcome this error and successfully update my Firestore document by adding an element to the 'attachments' array field using 'arrayUnion' while using AngularFire.


Solution

  • As @Hezy Ziv said I need just to use any in the addCarAttachment

    addCarAttachment(carId: string, attachment: CarAttachment) {
      return this.firestore.collection<any>('car').doc(carId).update({
        attachments: FieldValue.arrayUnion(attachment),
      });
    }