我想将当前用户ID推送到Firestore中的数组。到目前为止,我可以使用一个值更新userFavorite,但是我想每次用户单击“收藏夹”按钮时添加一个user.id值数组。
这是我当前拥有的代码。当用户上传帖子时,将创建userFavorite。现在,我想向数组添加多个user.id值。我该怎么办?
服务
updatePost(postKey, value){
return new Promise<any>((resolve, reject) => {
let currentUser = firebase.auth().currentUser;
// this.afs.collection('people').doc(currentUser.uid)
this.afs.collection('posts').doc(postKey).update(value)
.then(
res => resolve(res),
err => reject(err)
)
})
}
TS
favoriteUser(){
let currentUser = firebase.auth().currentUser;
let data = {
userFavorite : currentUser.uid
}
this.firebaseService.updatePost(this.item.id,data)
.then(
res => {
this.router.navigate(["/tabs/tab4"]);
}
)
}
HTML
<ion-button (click)="favoriteUser()">FavoriteProject</ion-button>
答案 0 :(得分:0)
如果要在单个字段中保留多个唯一值,则需要寻找arrayUnion
operator。
let currentUser = firebase.auth().currentUser;
let data = {
userFavorite : firebase.firestore.FieldValue.arrayUnion(currentUser.uid)
}
this.firebaseService.updatePost(this.item.id,data)