简体   繁体   English

更新所有用户 firebase angular 的特定字段

[英]Update specific field of all user firebase angular

Since I am new to angular and Ionic I am trying to update the specific field of user.由于我是 angular 和 Ionic 的新手,因此我正在尝试更新用户的特定领域。 I want to display all the data from the firebase and update only one field.我想显示来自 firebase 的所有数据并只更新一个字段。 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.我想显示数据库中每个用户的电子邮件,我想允许管理员只更新enableAccess字段。 How do i get all user and update the field?如何获取所有用户并更新该字段?

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.如果是这种情况,我建议使用一次性 node.js 脚本执行此操作,而不是在客户端应用程序代码中执行此操作。

In node.js this would look like:在 node.js 中,这看起来像:

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.鉴于这不会立即影响 UI,我建议通过常规 SDK 在 JavaScript 中进行回填。

Even if the result is shown in the Angular UI, AngularFire is built on top of the JavaScript SDK, so they interop perfectly.即使结果显示在 Angular UI 中,AngularFire 也是建立在 JavaScript SDK 之上的,因此它们可以完美地互操作。 This means that the updates will show up in AngularFire right away too, even when you make them through the (simpler) JavaScript SDK.这意味着更新也会立即显示在 AngularFire 中,即使您通过(更简单的)JavaScript SDK 进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM