您在需要流的地方提供了“未定义" [英] You provided 'undefined' where a stream was expected

查看:42
本文介绍了您在需要流的地方提供了“未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制台中得到了这个.因为它没有指向我的代码,所以我不知道它是怎么回事:

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)

有人可以帮忙吗?

更新.这是实际调用:

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

更新 2.这是我的拦截器最像发生的地方:

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

所以,如果我注释掉 .pipe,我不会收到消息.但如果有错误,我仍然想处理.

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

更新 3.这是初始调用:

Update 3. Here is the initial call:

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

推荐答案

就我而言,此错误是由循环引用"引起的.所以基本上,这个错误与 rxjs 没有任何关系.至少不是直接的,是json序列化出错了,所以这个错误实际上非常具有误导性.

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.

我基本上拥有的是 2 个对象:

What I basicly have were 2 objects:

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

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

当我检索一个 Persons 列表,有一个 Pets 列表时,我想将所有 pet.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));

现在我有了对象的循环引用:person1 的 pet1 链接到所有者 person1,其所有者 person1 的 pet1 链接到...等...

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...

当 person1 尝试序列化为 json 时,json 显然不知道在哪里停止并且可能返回一个未定义的.这个 undefined 被传递给 rxjs,在那里它需要一个流、可观察等,这会导致错误.

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.

因此,如果您曾经遇到此错误并且您检查了所有建议的选项,但没有任何建议有效,请检查您在 rxjs 操作中使用的对象.在我的例子中,它是 HttpClient.post,我将我想要发布到服务器的对象发送到那里.

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.

这篇关于您在需要流的地方提供了“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆