我想在firestore中的字段中保存一个值,就像这个图像一样。
正如您在图片中看到的,我希望shopId是动态的,然后它将包含具有布尔值的日期和时间字段。
我的目标是跟踪用户在不同商店的Checkin历史记录。希望你能理解我在这里说的话。
现在这是我目前的代码:
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!`);
});
}
};
这可能到firestore吗?
答案 0 :(得分: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 }
);