0

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 enter image description here

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);
}
7
  • 2
    Can you please share your code that is not working as intended? Commented Dec 17, 2021 at 17:48
  • That's an object, not an array. Commented Dec 17, 2021 at 17:49
  • @Dharmaraj I have updated the question Commented Dec 17, 2021 at 18:08
  • Remove the {merge: true}... It can be used only when you are using set() Commented Dec 17, 2021 at 18:09
  • 2
    Use the first one but either change update to set or remove {merge: true} from update Commented Dec 17, 2021 at 18:21

2 Answers 2

1

Since you set the entire 2022-01-03 field, you are overwriting any existing values in that field.

If you want to merge the new value/object you specify with the existing values in a field use the array-union operator.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response, I have updated the original question with my attempt and your solution. I am receiving an error
The array union did the trick once i figured it out, thank you !
0

Use push() to add to an array.

// 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,
});

4 Comments

Thanks for the reply, I have tried your solution and it is still overwriting the data
You should only have let array = {} once, at the beginning of the script. The other parts are done every time you want to add something.
I have updated the original question with your solution, if you could review please. it is still overwriting the data. Did i do it correctly?
I don't know firebase, so I can't help you with that part. But whenever you say you get an error, you need to post the exact error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.