0

I want to save a value inside a field in firestore, like this image.

enter image description here

As you can see in the picture, I want shopId to be dynamic, then it will contain date and time fields with a boolean value.

My objective is to track a user's Checkin history on different shops. Hope you understand what i'm saying here.

now this is my current code:

checkInShop() {
  let currentUser = firebase.auth().currentUser;
  if (currentUser) {
    const db = firebase.firestore();
    const batch = db.batch();
    const docRef = db.collection("userCheckInHistory").doc(currentUser.uid);
    const timestamp = firebase.firestore.FieldValue.serverTimestamp();
    batch.set(docRef, {
      `${this.state.shopId}`: {
        `${timestamp}`: true
      }
    });
    // Commit the batch
    batch.commit().then(function() {
      AlertIOS.alert(`ShopId: Sent!`);
    });
  }
};

Is this possible to firestore?

esaminu
  • 402
  • 1
  • 4
  • 9

1 Answers1

1

you can do:

docRef.set({ [this.state.shopId]: { [timestamp]: true } });

you probably want to set merge to true in this case as well:

docRef.set({ [this.state.shopId]: { [timestamp]: true } }, { merge: true });

if you have to use template literals:

docRef.set(
  { [`${this.state.shopId}`]: { [`${timestamp}`]: true } },
  { merge: true }
);
esaminu
  • 402
  • 1
  • 4
  • 9