Home > Net >  forkJoin return error You provided 'undefined' where a stream was expected
forkJoin return error You provided 'undefined' where a stream was expected

Time:10-12

Previously, I asked an issue about return nested httprequest with wait subrequest.

https://stackoverflow.com/questions/69308614/angular-map-wait-observable-to-complete[Angular map wait Observable to complete]1

I made some changes to it, but it throw error

Error: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

Here are my update code

      test2(): Observable<Student[]> {
    return this.test1().pipe(
      mergeMap((result) => {
        if (!result) {
          return of(result);
        }
        return forkJoin(
          result.map((rr) => {
            if (rr.age === 1) {
              rr.age = rr.age * 10;
              return of(rr);
            } else {
              this.client
                .get('https://api.coindesk.com/v1/bpi/currentprice.json')
                .pipe(
                  map((res) => {
                    console.log(res);
                    rr.age = rr.age * 10000;
                    return rr;
                  })
                );
            }
          })
        ).pipe(
          map((paths) => {
            console.log('w');
            console.log(paths);
            return paths;
            // return result.map((e, index) => ({
            //   age: paths[index],
            // }));
          })
        );
      })
    );
  }

CodePudding user response:

The issue in your code is that you are using a condition inside your .map. If you are using a condition in .map function, you need to add return because as the error states that a stream was expected instead of undefined. In order for you to provide an observable, you need to return your observable inside your if-else condition.

In the code below, I added the keyword return on your of() and on your http call. I also created a stackblitz containing my solution for you check.

test2(): Observable < Student[] > {
  return this.test1().pipe(
    mergeMap((result) => {
      if (!result) {
        return of(result);
      }
      return forkJoin(
        result.map((rr) => {
          if (rr.age === 1) {
            rr.age = rr.age * 10;
            return of(rr);
          } else {
            return this.client
              .get('https://api.coindesk.com/v1/bpi/currentprice.json')
              .pipe(
                map((res) => {
                  console.log(res);
                  rr.age = rr.age * 10000;
                  return rr;
                })
              );
          }
        })
      ).pipe(
        map((paths) => {
          console.log('w');
          console.log(paths);
          return paths;
        })
      );
    })
  );
}
  • Related