0

Since I am new to angular and Ionic I am trying to update the specific field of user. I want to display all the data from the firebase and update only one field. I was able to do for only one user that is login but not for all.

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 want to display email of every user in the database and I want to allow admin to update only enableAccess field. How do i get all user and update the field?

0

1 Answer 1

1

It sounds like you're trying to backfill some data for existing users. If that's the case, I'd recommend doing that with a one-time node.js script, instead of in the client-side application code.

In node.js this would look like:

const admin = require("firebase-admin");
admin.initializeApp(...);

const db = admin.firestore();
db.collection("user").get((snapshot) => {
  snapshot.forEach((doc) => {
    doc.ref.update({ IsApproved: false });
  });
});

If you insist on doing this in client-side code, it would be almost the same. Given that this is not immediately impacting the UI, I'd recommend doing the backfill in JavaScript through the regular SDK.

Even if the result is shown in the Angular UI, AngularFire is built on top of the JavaScript SDK, so they interop perfectly. This means that the updates will show up in AngularFire right away too, even when you make them through the (simpler) JavaScript SDK.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.