As a newcomer to Angular and Ionic, I am trying to update a specific field for a user. My goal is to retrieve all the data from Firebase and update only one field. I have successfully accomplished this for a single logged-in user, but I am facing difficulties when trying to do it for all users.
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
});
}
}
component
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>
I am looking to display the email of each user in the database and allow the admin to only update the enableAccess
field. How can I retrieve all users and update this field?