【问题标题】:Firestore insert object fieldFirestore 插入对象字段
【发布时间】:2018-01-31 08:12:25
【问题描述】:

我想在 firestore 的字段中保存一个值,就像这张图片一样。

正如您在图片中看到的,我希望 shopId 是动态的,然后它将包含带有布尔值的日期和时间字段。

我的目标是跟踪用户在不同商店的签到历史记录。希望你明白我在说什么。

现在这是我当前的代码:

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!`);
    });
  }
};

这可以火存储吗?

【问题讨论】:

标签: object react-native google-cloud-firestore


【解决方案1】:

你可以这样做:

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

在这种情况下,您可能也希望将 merge 设置为 true:

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

如果你必须使用模板文字:

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

【讨论】:

  • 现在我有另一个问题,关于使用 {merge: true} 如何在对象字段中实现它?
猜你喜欢
  • 2018-06-17
  • 2018-06-11
  • 2021-07-29
  • 2019-02-13
  • 2019-02-06
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多