Firebase Firestore add item issue

When you construct a custom type and trying to add to firestore.

with the following text book code:

constructor(private afs: AngularFirestore) {
  this.itemsCollection = afs.collection<Item>('items');
  this.items = this.itemsCollection.valueChanges();
}
add(item: Item){
  this.itemsCollection.add(item);
}

There will be an error:

Exception caught: (FirebaseError) : Function CollectionReference.add() requires its first argument to be of type object, but it was: a custom Item object

So need a simple conversion to JSON

add(item: Inventory){
  this.itemsCollection.add(JSON.parse(JSON.stringify(item)));
}

1 Comments

Leave Comment

Your email address will not be published. Required fields are marked *