I’m using Firebase
and Vuejs
to create an database element, which has object array inside.
That’s how the field looks, and I want to add tasks through the form into the ‘moreTasks’ as an array.
I tried using this, but it just creates new entity in the database.
JavaScript
x
4
1
db.collection('Tasks').add({
2
tasker: this.tasker.taskerName
3
})
4
I also tried checking API but I couldnt understand the refs, because I was using different methods to achieve that goal.
JavaScript
1
5
1
creatTask() {
2
db.collection('Tasks').add({
3
task_id: this.task_id,
4
name: this.name,
5
What would be correct way to approach this problem?
Advertisement
Answer
You can append an item to an array using FieldValue.arrayUnion() as described in the documentation. For example:
JavaScript
1
5
1
// Atomically add a new region to the "regions" array field.
2
washingtonRef.update({
3
regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
4
});
5