louay jaber
louay jaber

Reputation: 7

how to set data to firebase map object

I have a firebase map object and i need to insert arrays inside

I am trying to use update function but it doesn't work

dialogRef.afterClosed().subscribe(result => {
  console.log(result);

  const arrayname = result.name;
  const fireupdate = this.af.list('/users/' + this.Uid + '/items').update(arrayname, result);
}); 

this is how the result array looks like

{name: "item", price: 25, desc: "a simple item"}

I don't get anything in the firebase here is my firebase structure

Upvotes: 0

Views: 9187

Answers (2)

Swoox
Swoox

Reputation: 3740

So what is easiest to do is add the array to the doc as shown in the other answer.

this.db.collection("users").doc(this.Uid).update({items: [{name: "item", price: 25, desc: "a simple item"}, {name: "item2", price: 20, desc: "a simple item2"}]}) 

What you must be careful about is that firestore not really have a good way to query an array with objects yet:

query an array

What Firestore want you to do is basicly:

this.db.collection("users").doc(this.Uid).collection('items').add({name: "item", price: 25, desc: "a simple item"})

Upvotes: 4

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

If I am not mistaking, by doing this.af.list('/users/' + this.Uid + '/items').update() you are actually trying to write to the Realtime Database, see https://github.com/angular/angularfire2/blob/master/docs/rtdb/lists.md#updating-items-in-the-list-using-update

But the database that you show in your question is Firestore, the other database service offered by Firebase.

You should use the part of the angularfire2 library dedicated to Firestore, see https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md#using-angularfirestoredocument

Upvotes: 0

Related Questions