【问题标题】:How to retrieve information of a loan (loan document fields) and the items within the specific loan (items collection of loan document) from firebase?如何从firebase检索贷款信息(贷款文件字段)和特定贷款中的项目(贷款文件的项目集合)?
【发布时间】:2021-09-07 16:04:21
【问题描述】:

我正在尝试通过其 ID 和该贷款中项目的详细信息来获取特定贷款的信息,以将其显示在详细信息页面中。但是,我无法也不知道如何使用提供给我的贷款服务的 getLoanById 方法检索贷款和贷款项目。

detail.page.ts

export class DetailPage {
  loan: Loan;
  loanId: string;

  constructor(private route: ActivatedRoute, private loanService: LoanService) {
    this.loanId = this.route.snapshot.params.id;
    this.loan = this.loanService.getLoanById(this.loanId);
  }
}

loan.service.ts

  getLoanById(id: string) {
    return firebase.firestore().collection('loans').doc(id).get().then(doc => {
      let loan = new Loan(doc.data().username, doc.data().status, doc.data().duedate.toDate(), doc.id);

      return firebase.firestore().collection('loans/' + id + '/items').get().then(collection => {
        loan.items = []; // Empty array
        collection.forEach(doc => {
          let item = new Item(doc.id, doc.data().quantity);
          loan.items.push(item);
        })
        return loan;
      });
    });
  }

【问题讨论】:

    标签: angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    getLoanById()改成如下方法:

     async getLoanById(id: string) : Promise<any> {
        const doc = await firebase.firestore().collection('loans').doc(id).get();
        let loan = new Loan(doc.data().username, doc.data().status, doc.data().duedate.toDate(), doc.id);
        const collection = await firebase.firestore().collection('loans/' + id + '/items').get();
        loan.items = []; // Empty array
        collection.forEach(doc_1 => {
          let item = new Item(doc_1.id, doc_1.data().quantity);
          loan.items.push(item);
        });
        return loan;
      }
    

    由于get() 方法是异步的,因此使用async/await 等待直到检索到数据然后将其添加到列表中。然后在DetailPage 中,您可以执行以下操作:

       constructor(private route: ActivatedRoute, private loanService: LoanService) {
        this.loanId = this.route.snapshot.params.id;
        this.loanService.getLoanById(this.loanId).then((result){
          console.log(result);
         });
      }
    

    then() 方法返回一个Promise。它最多需要两个参数:Promise 的成功和失败情况的回调函数。一旦 Promise 被履行或拒绝,相应的处理函数(onFulfilled 或 onRejected)将被异步调用。

    检查:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

    【讨论】:

    • detail.page.html 现在的加载速度比从 firebase 检索的数据要快,导致页面仅部分加载。有没有办法在加载页面之前完全检索 Firebase 数据?
    • 是的,html 会在之前加载,因为此调用是异步的,通常因为您使用 angular 和 ionic,然后只需实现 OnInit 类并覆盖方法 ngOnInit() 调用 getLoanById()。还可以使用组件ionicframework.com/docs/api/loading 在加载数据之前显示一个小微调器,并在加载数据后将其关闭
    • 我已经实现了 OnInit 类,但我无法弄清楚如何重写方法 ngOnInit() 来调用 getLoanById()。
    • 实现之后是不是报错了?写ngOnInit() {}
    • 我确实写了 ngOnInit(){},但我应该在 ngOnInit 方法中写任何东西,因为 html 仍然首先加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多