Reputation: 10870
I have the following model for a Firestore document:
{
name: "test",
isActive: true,
items: [
{
id: "123",
itemName: "testItem",
qty: 1
},
{
id: "555",
itemName: "anotherItem",
qty: 5
}]
}
Now that Firestore allows to work with nested arrays (via arrayRemove / arrayUnion
), I am wondering whether this is possible also through AngularFire2.
Or the only way so far is to import Firebase and using it straight ahead?
Like:
this.firestore.collection(<collectionName>).doc(<docID>).update({
answers: firestore.FieldValue.arrayUnion(<AnswersObject>)
});
Moreover is such a model with nested array legit in FIrestore or should it rather be structured in a different way?
Upvotes: 1
Views: 338
Reputation: 1097
I struggled with this a while, it seems angular's documentation is always behind. I updated an item the array like this, the array is called accomodation
and it is within the day
object.
this.data.firestore.doc(`days/${accommodation.day}`)
.update({
accommodation: [
accommodation
]
});
Upvotes: 1
Reputation: 5272
Unfortunately, AngularFire2 does NOT yet support this. You can verify this by checking out the source code.
Or, if you're lazy like me and don't feel like digging through trying to find it on GitHub... what I did to double check was to download the whole repo as a ZIP file, extract, and open folder in VS Code. Searching the whole folder for FieldValue
or arrayUnion
returns nothing - those words don't exist in the entirety of the source.
So for right now, you're correct about needing to stick with the default Firebase/Firestore package. And nested arrays are definitely a "legit" thing, but like everything else, when to use it depends on your situation - and I don't feel qualified or experienced enough to evaluate your situation and make a strong recommendation.
Upvotes: 1