简体   繁体   English

如何订阅 Observable Forkjoin 数组不起作用

[英]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.我正在尝试使用 forkjoin 以 angular 方式执行多个 HTTP 请求。但是当我订阅 Observable 时,它​​没有给出任何响应。

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

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.好的,如果您在没有帮助的 map 函数的情况下无法订阅,我认为正在发生的事情是您的 get_student_xxx 函数没有返回可观察的。

If that function returns an observable you will then be able to subscribe to it.如果该函数返回一个 observable,您就可以订阅它。

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?所以这给了你几个选择......也许你只需要改变返回值而不是使用 Observable 和订阅?

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:还有一些方法可以创建您自己的 observable,例如 rxjs 有一个名为“of”的函数,您可以使用它来将对象“包装”在一个 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此外,如果您的函数返回一组 observables 是否有可能 [data] 应该只是数据

As of now, I didn't get any answer.so now I reinitialized vanilla firebase in component.ts and try it works for me.到目前为止,我没有得到任何答案。所以现在我在 component.ts 中重新初始化了 vanilla firebase 并尝试它对我有用。 if anyone solves this problem using angular fire please let me know.如果有人使用 angular fire 解决了这个问题,请告诉我。 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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM