Firebase是否可以通过Cloud Firestore将变量设置为字段名称?
例如,我想使用一种功能将数据存储为这样:
Madonna: true
Queen: false
MJackson: true
下面的功能不起作用,但是说明了我要实现的目标:
setFavorite(
channelID: string,
favorite: boolean
): Promise<void> {
return this.fireStore.firestore.doc(`favorites/${mycustomID}`)
.set({
`${channelID}`: favorite
});
}
答案 0 :(得分:2)
您应使用square brackets notation,如下所示:
setFavorite(
channelID: string,
favorite: boolean
): Promise<void> {
let dataObj = {};
dataObj[channelID] = favorite;
return this.fireStore.firestore
.doc(favorites/${mycustomID})
.set(dataObj);
}