You provided 'undefined' where a stream was expected

孤街浪徒 提交于 2020-06-17 10:01:13

问题


I am getting this in my Console. Since it's not pointing to my code I have no idea what it's all about:

TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:28)
at subscribeToResult (subscribeToResult.js:15)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:43)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/OuterSubscriber.js.OuterSubscriber.notifyError (OuterSubscriber.js:13)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._error (InnerSubscriber.js:18)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:59)
at Observable._subscribe (throwError.js:5)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:43)
at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:29)
at subscribeToResult (subscribeToResult.js:13)

Can someone please help?

Update. Here is the actual call:

this.http.get(`${environment.apiUrl}/api/home/`)
   .subscribe((data: any) => {console.log(data);
});

Update 2. Here is my interceptor where it's most like happening:

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  // add authorization header with jwt token if available
  request = this.addTokenToRequest(request);
  return next.handle(request);
  // .pipe(
  //   catchError((error: HttpErrorResponse) => {
  //     if (error instanceof HttpErrorResponse) {
  //       switch (error.status) {
  //         case 406: {
  //           return this.handle406Error(request, next);
  //         }
  //         // case 'Unauthorized': {
  //         //   // return this.handle401Error(request, next);
  //         // }
  //     }}
  //   }));
}

So, if I comment out .pipe I don't get the message. But I still want to process errors if any.

Update 3. Here is the initial call:

ngOnInit(): void {    
    this.http.get(`${environment.apiUrl}/api/web/home/`)
      .subscribe((data: any) => {
        console.log(data);
    });
}

回答1:


In my case, this error was caused by a "circular reference". So basicly, the error didn't had anything to do with rxjs. Not directly at least, it was the json serialization that went wrong so the error was actually very misleading.

What I basicly have were 2 objects:

class Person {
    id: number;
    pets: Pet[];
}

class Pet {
    id: number;
    owner: Person;
}

When I retrieved a list of Persons, having a list of Pets, I wanted to relate all the pet.owner to their parent Person as these would not be the same by reference after retrieving from the server. So I had this code:

persons.forEach(person => person.pets.forEach(pet => pet.owner = person));

Now I have the circular reference of objects: person1 has pet1 which is linked to owner person1 which has pet1 which is linked to.... etc...

When person1 is attempt to get serialized to json, json doesn't know where to stop obviously and probably returns an undefined. And this undefined is passed to the rxjs where it expects a stream, observable etc. which is results in the error.

So, if you ever run in this error and you checked all the options that have been suggested but none of the suggestions works out, check the object you use in your rxjs operations. In my case it was the HttpClient.post where I sent along the object I wanted to post to the server.



来源:https://stackoverflow.com/questions/58860785/you-provided-undefined-where-a-stream-was-expected

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