Mohd Sabban
Mohd Sabban

Reputation: 192

How to subscribe array of Observable Forkjoin not working

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();
  }

Upvotes: 1

Views: 1486

Answers (2)

Mohd Sabban
Mohd Sabban

Reputation: 192

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 = [
          '[email protected]',
          '[email protected]',
          '[email protected]',
        ];
        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
  }

Upvotes: 0

JBoothUA
JBoothUA

Reputation: 3149

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

Upvotes: 1

Related Questions