0

So right now I am currently storing data with a number after it like EmailSubject1, EmailSubject2, etc which obviously is a stupid way of doing things.

I would instead like to store these in arrays like EmailSubject[1], EmailSubject[2] etc.

I spent some time looking over the firebase documentation and searching online but was struggling to find a solution and I am unfamilar in storing things in firebase this way personally.

   let z = 0;
              for (z = 0; z < 20; z++) {
                db.collection("Users")
                  .doc("6IzsLbD4r4R5RXdGB5BQy6xq8Dc2")
                  .set(
                    {
                      ["EmailSubject" + z]: mail.subject,
                      ["EmailBody" + z]: mail.text,
                      ["EmailFrom" + z]: mail.from,
                      ["EmailDate" + z]: newDate,
                    },
                    { merge: true }
                  )

                  .then(() => {
                    console.log("Doc successful");

                    return null;
                  })

                  .catch((error) => {
                    console.error("Error writing doc", error);
                  });
              }


Here is my code, any help would be much appreciated =]

3
  • 1
    Can you show the document/fields structure you are exactly looking for? Commented Apr 7, 2020 at 7:31
  • Probably to have 1 main EmailSubject, EmailBody, EmailFrom, EmailDate. And then inside of each of those, EmailSubject[1], EmailSubject[2] etc as it loops and keeps adding the data. Does that make sense? I am storing many emails and these are things I am storing from them. @RenaudTarnec Then I will know EmailSubject[1] and EmailFrom[1] go together. Commented Apr 7, 2020 at 7:34
  • See the answer. Note that Array's index starts at 0. Commented Apr 7, 2020 at 7:48

1 Answer 1

1

If I correctly understand your question, you can use arrayUnion() to add elements to the different arrays, but you should note that this method will only add elements that are not already present.

So if you are sure that you only add new data, you could do as follows:

  var promises = [];
  var docRef = db
    .collection('Users')
    .doc('6IzsLbD4r4R5RXdGB5BQy6xq8Dc2');

  for (z = 0; z < 20; z++) {
    promises.push(
      docRef.set(
        {
          EmailSubject: firebase.firestore.FieldValue.arrayUnion(
            mail.subject + z
          ),
          EmailBody: firebase.firestore.FieldValue.arrayUnion('text' + z),
          //....
        },
        { merge: true }
      )
    );
  }
  Promise.all(promises);

If, on the opposite, there are values that are similar in one of the Arrays, you would need to read the Array in your client, push the new value and write back the entire Array.

Ex.: If your EmailFrom array is ['[email protected]', '[email protected]', '[email protected]'], arrayUnion() will not work.

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

Comments

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.