Observable Forkjoin이 작동하지 않는 배열을 구독하는 방법

Nov 18 2020

forkjoin을 사용하여 각도로 여러 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');

여기 내 서비스입니다

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

답변

1 JBoothUA Nov 19 2020 at 06:21

좋습니다. 도움이되는지도 기능없이 구독 할 수 없다면 get_student_xxx 함수가 관찰 가능 항목을 반환하지 않는 것 같습니다.

해당 함수가 Observable을 반환하면 구독 할 수 있습니다.

트랜스 파일러에서 추가 도움을 받기 위해 이렇게 할 수 있습니다.

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

그러면 몇 가지 옵션이 제공됩니다. 반환 값을 변경하고 Observable을 사용하지 않고 구독해야 할 수도 있습니다.

또는 관찰 가능한 다른 객체를 반환해야 할 수도 있습니다.

자신 만의 옵저버 블을 생성하는 방법도 있습니다. 예를 들어 rxjs에는 옵저버 블에서 객체를 "래핑"하는 데 사용할 수있는 "of"라는 함수가 있습니다.

// 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));

또한 함수가 관찰 가능한 배열을 반환하는 경우 [데이터]가 데이터 여야 할 수 있습니다.

MohdSabban Nov 26 2020 at 05:25

지금은 답을 얻지 못 했으므로 이제 component.ts에서 바닐라 파이어베이스를 다시 초기화하고 나를 위해 작동합니다. 누군가가 각도 불을 사용 하여이 문제를 해결하면 알려주십시오. 여기 내 해결책이 있습니다

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
  }