I have my current code
let array = {};
array[date] = [
{
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
},
];
that uploads to firebase like this

but every time i add a new entry for the same date, it overwrites the existing data. I want the data to be add to map "1" etc instead of overwriting the data in map "0"
update: I have tried the following and I am receiving an error. Not sure if I did this correctly, I am still learning.
let array = {};
array[date] = [];
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.update(
{
array: firebase.firestore.FieldValue.arrayUnion({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
}),
},
{ merge: true }
);
} catch (e) {
alert(e);
console.log(e);
}
update with push:
// at beginning of script
let array = {};
// later, when you want to add to it
if (!array[date]) {
array[date] = [];
}
array[date].push({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
});
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.set(
array,
{ merge: true }
);
} catch (e) {
alert(e);
console.log(e);
}
},
Solution:
try {
await firebase
.firestore()
.collection("Notifications")
.doc(`${helper}`)
.update({
[date]: firebase.firestore.FieldValue.arrayUnion({
Name: Name,
Cell: Cell,
Age: Age,
userId: userId,
Time: time,
Type: type,
read: false,
}),
});
} catch (e) {
alert(e);
console.log(e);
}
{merge: true}... It can be used only when you are usingset()updatetosetor remove{merge: true}from update