【发布时间】:2021-12-01 12:47:45
【问题描述】:
由于我是 Angular 和 Ionic 的新手,我正在尝试更新用户的特定字段。我想显示来自 firebase 的所有数据并只更新一个字段。 我只能为一个登录的用户做,但不能为所有用户做。
export class AdminPage implements OnInit {
user:any;
userId:string;
enableAccess:boolean;
constructor(
private auth:AuthService,
private router:Router,
private afs:AngularFirestore,
private loadingCtrl:LoadingController,
private toastr:ToastController) { }
ngOnInit() {
this.auth.getAllUser().subscribe(user=>{
this.user=user;
})
this.auth.user$.subscribe(user=>{
this.enableAccess=user.IsApproved;
})
}
async updateUserInfo(){
const loading=await this.loadingCtrl.create({
message:'updating',
spinner:'crescent',
showBackdrop:true
})
loading.present()
this.afs.collection('user').doc(this.userId).set({
'IsApproved':this.user.enableAccess
},{merge:true}).then(()=>{
this.toast('update sucessful','success');
}).catch(error=>{
loading.dismiss();
this.toast(error.message,'danger');
})
}
async toast(message,status){
const toast =await this.toastr.create({
message:message,
color: status,
position: 'top',
duration:2000
});
}
}
组件
ion-content>
<ion-card>
<ion-item>
<ion-label position="floating">Email:</ion-label>
<p></p>
</ion-item>
<ion-item>
<ion-label position="floating">Enable Access:</ion-label>
<ion-input
required
type="boolean"
[(ngModel)]="enableAccess"
name="enableAccess"
></ion-input>
</ion-item>
<ion-button
type="submit"
expand="block"
shape="round"
(click)="updateUserInfo()"
>Update</ion-button
>
</ion-card>
</ion-content>
我想显示数据库中每个用户的电子邮件,并且我想允许管理员仅更新 enableAccess 字段。如何获取所有用户并更新字段?
【问题讨论】:
标签: angular typescript firebase ionic-framework google-cloud-firestore