简体   繁体   English

如何在运行函数之前等待数据从 Firestore 加载 Angular

[英]How to wait for data to be loaded from Firestore before running functions Angular

Current situation:现在的情况:

I have a service that reads the data from Cloud Firestore from employee.service.ts :我有一项服务可以从employee.service.ts读取来自 Cloud Firestore 的数据:

getEmployeeList(orgID: string) {
    return this.firestore.collection('orgData').doc(orgID).collection('employeeList').snapshotChanges();
  }

I use this service to read the data in my component from component.ts :我使用此服务从component.ts读取组件中的数据:

export class ManageEmployeesComponent implements OnInit {
    newEmployee: FormGroup;
    Employees: Employee[];

    selectedOrganization: string;
  
    @Input() set selectOrg(org: string) {
      this.selectedOrganization = org;
      this.getEmployees();
      this.formsInit();
    }

    getEmployees() {
      this.employeeService.getEmployeeList(this.selectedOrganization).subscribe(data => {
        this.Employees = data.map(e => {
          return {
            id: e.payload.doc.id,
            ...e.payload.doc.data() as Employee
          };
        })
      });
    }

    formsInit() {
        //some reactive form stuff
    }
}

The problem:问题:

Every time the selectOrg input gets set, it runs getEmployees and formsInit synchronously.每次设置selectOrg输入时,它都会同步运行getEmployeesformsInit The problem is that I need some of the data from getEmployees() to be completely loaded before I run formsInit() .问题是我需要在运行formsInit()之前完全加载来自getEmployees()的一些数据。

Question问题

How do I rewrite this code so that formsInit waits for getEmployees to complete its async operations?我如何重写这段代码,以便formsInit等待getEmployees完成其异步操作?

Edit 1:编辑 1:
The best I can think right now is to modify getEmployees() to accept an optional argument that is a function for it to run once the data is retrieved.我现在能想到的最好的办法是修改getEmployees()以接受一个可选参数,即 function 以便它在检索到数据后运行。 Not sure how practical or clean that would be, though.不过,不确定那会有多实用或干净。

You could just call formsInit() from your getEmployees function like so:您可以像这样从getEmployees function 调用formsInit()

export class ManageEmployeesComponent implements OnInit {
    newEmployee: FormGroup;
    Employees: Employee[];

    selectedOrganization: string;
  
    @Input() set selectOrg(org: string) {
      this.selectedOrganization = org;
      this.getEmployees();
    }

    getEmployees() {
      this.employeeService.getEmployeeList(this.selectedOrganization).subscribe(data => {
        this.Employees = data.map(e => {
          return {
            id: e.payload.doc.id,
            ...e.payload.doc.data() as Employee
          };
        });
        this.formsInit();
      });
    }

    formsInit() {
        //some reactive form stuff
    }
}

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

相关问题 在返回所有异步函数以更新 firestore 文档之前,如何等待 NodeJS 中的多个异步函数? - How do I wait for multiple async functions in NodeJS before returning them all to update a firestore document? 在viewcontroller中更新tableView之前等待单独的数据class从firestore获取数据? - Wait for separate data class to get data from firestore before updating tableView in viewcontroller? 在 xcode/swift 中运行 UITableView 函数之前,如何从 firebase 加载所有数据 - How to load all data from firebase, before running UITableView functions in xcode/swift 如何从云功能中的 firestore 获取文档 - How to get a document from firestore in cloud functions 如何等待数据可用 - flutter - How can I wait for data before it is available - flutter 如何使用 firebase 云函数对 Firestore 数据进行逻辑处理 - How to use firebase cloud functions for logic on firestore data 如何在验证电话号码之前访问 Firestore 数据? - How can I access firestore data before authenticating phone number? 如何从 Firestore 获取所有文档并进行循环(云功能) - how to get all documents from Firestore and make a loop (Cloud Functions) Android firestore 更改数据加载方式 - Android firestore changing the way data is loaded Firestore promise 在返回产品之前等待产品详细信息 - Firestore promise wait for product details before returning products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM