EmptyError:序列中没有元素 [英] EmptyError: no elements in sequence

查看:31
本文介绍了EmptyError:序列中没有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的 Angular2 应用程序从 2.0.0 升级到 2.3.0,但遇到了以下错误.关于为什么的任何想法?我看到了另一个帖子(Angular 2.0.1 Router EmptyError: 没有元素顺序),但是尝试了这个之后,问题仍然存在.导致此错误的原因是什么?

I'm upgrading my Angular2 app from 2.0.0 to 2.3.0, and I'm running into the following error. Any ideas as to why? I saw this other post (Angular 2.0.1 Router EmptyError: no elements in sequence), but after trying this, the issue still remains. What causes this error?

Error: Uncaught (in promise): EmptyError: no elements in sequence
Error: no elements in sequence
    at EmptyError.ZoneAwareError (zone.js:672)
    at new EmptyError (EmptyError.ts:13)
    at FirstSubscriber._complete (first.ts:161)
    at FirstSubscriber.Subscriber.complete (Subscriber.ts:122)
    at Subject._subscribe (Subject.ts:109)
    at Subject.Observable.subscribe (Observable.ts:98)
    at Observable._subscribe (Observable.ts:158)
    at Observable.subscribe (Observable.ts:98)
    at Observable._subscribe (Observable.ts:158)
    at FirstOperator.call (first.ts:82)
    at Observable.subscribe (Observable.ts:96)
    at Object.subscribeToResult (subscribeToResult.ts:32)
    at MergeAllSubscriber._next (mergeAll.ts:82)
    at MergeAllSubscriber.Subscriber.next (Subscriber.ts:95)
    at MapSubscriber._next (map.ts:80)
    at resolvePromise (zone.js:475) [angular]
    at resolvePromise (zone.js:460) [angular]
    at /libs/zone.js/dist/zone.js:509:17 [angular]
    at Object.onInvokeTask (core.umd.min.js:32) [angular]
    at ZoneDelegate.invokeTask (zone.js:261) [angular]
    at Zone.runTask (zone.js:151) [<root> => angular]
    at drainMicroTaskQueue (zone.js:405) [<root>]
    at ZoneTask.invoke (zone.js:336) [<root>]ErrorHandler.handleError @ core.umd.min.js:31next @ core.umd.min.js:32generatorOrNext.object.schedulerFn @ core.umd.min.js:32SafeSubscriber.__tryOrUnsub @ Subscriber.ts:238SafeSubscriber.next @ Subscriber.ts:190Subscriber._next @ Subscriber.ts:135Subscriber.next @ Subscriber.ts:95Subject.next @ Subject.ts:61EventEmitter.emit @ core.umd.min.js:32NgZone.triggerError @ core.umd.min.js:32onHandleError @ core.umd.min.js:32ZoneDelegate.handleError @ zone.js:233Zone.runGuarded @ zone.js:129_loop_1 @ zone.js:416drainMicroTaskQueue @ zone.js:425ZoneTask.invoke @ zone.js:336

我还注意到,当我尝试更改为使用 Guards 的路由时,会抛出 NavigationError 对象.但只有上面的错误是控制台中显示的内容.

I've also noticed that a NavigationError object is thrown when I try to change to a route that uses Guards. But only the error above is what is shown in the console.

NavigationError {id: 2, url: "/home", error: EmptyError}

我有点不知所措,希望得到任何帮助.

I'm kind of at a loss and would appreciate any help.

推荐答案

就我而言,当我在 Observable 中使用 first() 时遇到这些错误在 first() 之前发出了 takeUntil().

In my case I've got those errors when I used first() in an Observable that had a takeUntil() emitted before first().

示例:

let test$ = new ReplaySubject(1);
let testAux$ = new ReplaySubject(1);
let test2$ = test$.asObservable().takeUntil(testAux$.asObservable());
test2$.first().subscribe(() => console.log('first!'));
testAux$.next(null);

很难发现问题,因为我在不同类(在不同文件中)使用的服务中返回了一个带有 takeUntil()observable调用了 first(),并且在页面导航期间收到了错误(因为 takeUntil() 收到了一个 observable 在前一页被销毁时发出的),而且问题出在导航本身.

It was hard to discover the problem because I returned an observable with takeUntil() in a service that was used by a different class (in a different file) that called first(), and the error was received during page navigation (because takeUntil() received an observable that emitted when the previous page was destroyed), and it appeared as the problem was in the navigation itself.

最奇怪的是,错误发生在 next() 被调用时,而不是在 subscribe() 的第二个参数中,导致导航过程本身失败.我不知道 rxjs 团队是否选择了这个功能来表现这种行为,因为它使隔离问题变得更加困难,而且主要问题是,生产者由于某个人做了某事而收到错误消费者,这在我看来是一个糟糕的设计.

The weirdest thing is that the error happens when the next() is called, and not in the 2nd argument of subscribe(), causing the navigation process itself to fail. I don't know if this feature was chosen by the rxjs team to behave this way, because it makes much more difficult to isolate the problem, and, the main problem, the producer receives an error because of something done by a consumer, which seems as a bad design to me.

根据此评论:

.first()将只发出一项或抛出错误[...] 如果您想从 observable 中获得最多一项,请使用 .take(1).

.first() will emit exactly one item or throw an error[...] If you would like to get at most one item from the observable, use .take(1).

更新 (2020-08-31)

似乎可以通过在订阅中包含第二个参数来接收错误来实际处理错误,如下例所示:

It seems that the error can actually be handled by including a 2nd argument to receive errors in the subscription, like in the following case:

const test$ = new ReplaySubject(1);
const testAux$ = new ReplaySubject(1);
const test2$ = test$.asObservable().takeUntil(testAux$.asObservable());
test2$.first().subscribe(() => console.log('first!'), () => console.log('first error'));
testAux$.subscribe(() => console.log('another'), () => console.log('another error'));
console.log('1');
testAux$.next(null);
console.log('2');

那个日志:

1
first error
another
2

(在这种情况下,您不会看到 No elements in sequence 错误)

(in this case you wouldn't see the No elements in sequence error)

但是如果您最多想收到 1 个项目(但没有收到也可以),那么 take(1) 仍然是更好的方法,这就是我想要的默认推荐,而不是 first().

But if you want to receive at most 1 item (but it would still be fine to receive none), then take(1) would still be the better approach, and it is what I would recommend by default, instead of first().

这篇关于EmptyError:序列中没有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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