本文介绍了forkjoin后无法订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 angular 的循环内执行一些 firebase 操作.当我向 firebase 发送 HTTP 请求时,它会返回一组可观察值,因此使用 forkjoin 将这个可观察值数组转换为单个可观察值.现在的问题是,当我订阅这些新的 observables 时,我什么也没得到

 get_student_email() {this.get_project_service.get_project_by_doc_id(this.batchID, this.projectID).subscribe((res) => {this.response = res;this.email = this.response.student;让数据 = this.email.map((email) =>this.get_student_service.get_student_by_email_id(email));让信息 = forkJoin([数据]);console.log('info:', info);info.subscribe((res) => {控制台日志(res);console.log('执行');});});}输出:信息:可观察 {_isScalar:false,_subscribe:ƒ}

我也试试这个.这次它再次返回 observables

 let data = this.email.map((email) =>this.get_student_service.get_student_by_email_id(email));让信息 = forkJoin([数据]);console.log('info:', info);info.subscribe((res) => {控制台日志(res);console.log('执行');});});输出:-信息:可观察 {_isScalar: false, _subscribe: ƒ}[可观察]执行

这是我的服务

 get_student_by_email_id(email) {返回 this.fire_store.collection('用户', (ref) => ref.where('email', '==', email)).valueChanges();}

我不知道这个问题背后的主要原因是什么.我想要的是,在循环完成其执行后,它返回单个数组对象,其中包含有关属于该电子邮件数组的所有用户的信息(this.email)

解决方案

问题在于代码forkJoin([data]).

你的 data 它已经是一个 Observables 的数组,所以你只需将它传递给 forkJoin 就像这样:forkJoin(data).>

I want to perform some firebase operation inside a loop in angular. When I send an HTTP request to firebase it returns an array of observables so using forkjoin i convert this array of observables into single observables. Now the problem is when I subscribe to these new observables I didn't get anything

  get_student_email() {
    this.get_project_service
      .get_project_by_doc_id(this.batchID, this.projectID)
      .subscribe((res) => {
        this.response = res;
        this.email = this.response.student;
        let data = this.email.map((email) =>
          this.get_student_service.get_student_by_email_id(email)
        );
        let info = forkJoin([data]);
        console.log('info:', info);
        info.subscribe((res) => {
          console.log(res);
          console.log('Execute');
        });
      });
  }

Output:info: Observable {_isScalar: false, _subscribe: ƒ}

I also try this. In this time it again returns observables

 let data = this.email.map((email) =>
          this.get_student_service.get_student_by_email_id(email)
        );

        let info = forkJoin([data]);
        console.log('info:', info);
        info.subscribe((res) => {
          console.log(res);
          console.log('Execute');
        });
      });

Output:-
info: Observable {_isScalar: false, _subscribe: ƒ}
[Observable]
Execute

Here is my service

  get_student_by_email_id(email) {
    return this.fire_store
      .collection('User', (ref) => ref.where('email', '==', email))
      .valueChanges();
  }

I don't know what is the main reason behind this problem. What I want is that after loop complete its execution it return single array object contain information about all the user which belongs to that email array(this.email)

解决方案

The problem is with the code forkJoin([data]).

Your data it's already an array of Observables, so you just pass it to forkJoin like this: forkJoin(data).

这篇关于forkjoin后无法订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 20:38