How to subscribe array of Observable Forkjoin not working

喜欢而已 提交于 2020-12-13 03:40:48

问题


I am trying to do multiple HTTP requests in angular using forkjoin.But when I subscribe to Observable it doest not give any response.

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

        forkJoin([data]).subscribe((res) => {
          console.log(res);
        });

        console.log('Execute');

Here is my service

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

回答1:


Ok, if you're getting unable to subscribe without the map function that helps, I think what is going on is that your get_student_xxx function is not returning an observable.

If that function returns an observable you will then be able to subscribe to it.

you could do this to get some extra help from the transpiler:

get_student_by_email_id(email) : Observable<someobject> {
...
}

So that gives you a couple of options... Maybe you need to just change the return value and not use Observable and subscribe?

Or maybe you need to return a different object that is an observable.

There are also ways to create your own observable, for example rxjs has a function called "of" that you can use to "wrap" an object in an observable:

// RxJS v6+
import { of } from 'rxjs';
//emits any number of provided values in sequence
const source = of(1, 2, 3, 4, 5);
//output: 1,2,3,4,5
const subscribe = source.subscribe(val => console.log(val));

also if your function returns an array of observables is it possible that [data] should just be data




回答2:


As of now, I didn't get any answer.so now I reinitialized vanilla firebase in component.ts and try it works for me. if anyone solves this problem using angular fire please let me know. here is my solution

const firebaseapp = firebase.initializeApp(environment.firebase);

get_student_email() {
    let store = firebaseapp.firestore();
    this.get_project_service
      .get_project_by_doc_id(this.batchID, this.projectID)
      .subscribe(async (res) => {
        this.response = res;
        this.email = [
          'admin@gmail.com',
          'test01@gmail.com',
          'intern01@gmail.com',
        ];
        const information = await Promise.all(
          this.email.map(async (i) => {
            let a = await store
              .collection('User')
              .where('email', '==', i)
              .get();
            let collection;
            a.forEach((element) => {
              collection = element.data();
            });
            return collection;
          })
        );
        //  request end
        console.log(information);
      });
    // subscriber End
  }



来源:https://stackoverflow.com/questions/64905608/how-to-subscribe-array-of-observable-forkjoin-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!