【问题标题】:How to call a function after subscriber complete in angular 8+订阅者在 Angular 8+ 中完成后如何调用函数
【发布时间】:2020-12-16 08:26:05
【问题描述】:

大家好,我正在学习 Angular 和 Firebase。所以我的想法是我从我的票集合中获取一些票,并在获取后在其中添加一些新属性,但问题是当我收到一半数据时,我的排序函数调用(负责添加属性的函数)。或者简单地说,我们可以说我以流的形式接收数据。

 get_ticket() {
  console.log('have permission');
  this.get_ticket_service
    .get_ticket_company(this.user.company)
    .subscribe((res) => {
      console.log('get response');
      this.unStructure_ticket = res.map((e) => {
        return {
          id: e.payload.doc.id,
          item: e.payload.doc.data(),
        };
      });
      this.odering_ticket(this.unStructure_ticket)
    });

排序函数

  odering_ticket(data) {
    const info = [];
    console.log('hi');
    data.map((ticket) => {
      if (ticket.item.seen == false) {
        ticket.item.ticketID = ticket.id;
        ticket.item.ticketType = 'new';
        info.push(ticket.item);
      } else if (
        ticket.item.new == true &&
        ticket.item.currently_handle_sme_ID == undefined
      ) {
        ticket.item.ticketID = ticket.id;
        ticket.item.ticketType = 'not assigned';
        info.push(ticket.item);
      } else if (
        ticket.item.currently_handle_sme_ID == localStorage.getItem('uid') &&
        !this.has_view_all_ticket_permission
      ) {
        ticket.item.ticketID = ticket.id;
        ticket.item.ticketType = 'assigned';
        info.push(ticket.item);
      } else if (
        ticket.item.currently_handle_sme_ID != undefined &&
        this.has_view_all_ticket_permission
      ) {
        ticket.item.ticketID = ticket.id;
        ticket.item.ticketType = 'assigned';
        info.push(ticket.item);
      }
    });
    console.log('end map');
    return info;
  }

service.ts

  get_ticket_company(company) {
    return this.firebase_store
      .collection('Ticket', (ref) => ref.where('company', '==', company))
      .snapshotChanges();
  }

输出

有权限, 得到回应, 你好 , 结束地图, 得到回应, 你好 , 结束地图,

我想在收到所有数据后调用我的排序函数

【问题讨论】:

  • get_ticket_company 返回的可观察对象完成后,您已经在调用odering_ticketget_ticket_company 会发出多次吗?
  • 是的,两次排序函数调用

标签: javascript angular firebase google-cloud-firestore angularfire2


【解决方案1】:

您必须尝试使用​​last RxJs operator,这将使您的管道等到最后一个发出的值。

 this.get_ticket_service
    .get_ticket_company(this.user.company).pipe(
        tap((poRes) => {
            console.log('get response');
            if (this.unStructure_ticket === undefined) {
                // Initialize your data structure.
                this.unStructure_ticket = res.map((e) => {
                    return {
                        id: e.payload.doc.id,
                        item: e.payload.doc.data(),
                    };
                });
            } else {
                // Add or concate the data received
                // this.unStructure_ticket.push()//;
            }
        }),
        last(), // Wait for the last emitted value.
        tap((poRes) => {
            this.odering_ticket(this.unStructure_ticket);
        }),
    ).subscribe();

这应该如何工作的示例,但未经测试的代码。

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 2019-11-21
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多