user12051965
user12051965

Reputation: 147

Firestore add new map keys

I have a map in Firestore that I filled with new messages such as keys and Timestamp as values, something like:

history: {
  "user accepted this item": new Date(),
  "user used this item": new Date()
}

And here is my code, I want to update the map with a new value but it seems that I cannot use ${} directly there since it throws syntax error, but if I create an intermediary variable like I did with historyAction, Text Editor tells that variable is declared but not used, and as I understand correctly the behavior of updating Firestore data, this will update historyAction field that does not exist insted of the value it has. What should I do here?

let historyAction = `${user.displayName} accepted this item`;

        firebase
          .firestore()
          .collection("orders")
          .doc(orderId)
          .set({
              history: {
                historyAction: new Date().toISOString()
              }
            }, { merge: true }
          );

Upvotes: 1

Views: 1568

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

You should call out the full path of the individual string to update using dot notation, as illustrated in the documentation.

        firebase
          .firestore()
          .collection("orders")
          .doc(orderId)
          .set({
            [`history.${historyAction}`]: new Date().toISOString()
          }, { merge: true }
          );

Upvotes: 2

Related Questions