Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RxJS first()用于Observable.of()-序列中没有元素_Javascript_Angular_Rxjs_Ngrx - Fatal编程技术网

Javascript RxJS first()用于Observable.of()-序列中没有元素

Javascript RxJS first()用于Observable.of()-序列中没有元素,javascript,angular,rxjs,ngrx,Javascript,Angular,Rxjs,Ngrx,对于我的测试,我试图用Observable.of()模拟事件流,但是当我尝试 const actions$ = Observable.of({}); ... // in the function that is tested actions$ .filter(action => action.type === 'LOAD_REQUEST') .first() .subscribe(() => { ... do something }); 我得到以下错误 EmptyEr

对于我的测试,我试图用
Observable.of()
模拟事件流,但是当我尝试

const actions$ = Observable.of({});
...
// in the function that is tested
actions$
  .filter(action => action.type === 'LOAD_REQUEST') 
  .first()
  .subscribe(() => { ... do something });
我得到以下错误

EmptyError:xxx.js中没有顺序元素

这仅在我使用
.first()
时发生

如何模拟事件流,使测试不会失败?

的文档说明:

如果观察对象在发送下一个通知之前完成,则将EmptyError传递给观察者的错误回调

因此发生错误是因为您的测试数据没有通过
filter()
操作符并立即发出complete。

将只发出一个项目或抛出一个错误(如果未提供
defaultValue
参数),因此在空的可观察对象上调用它将导致错误。这是基于文档的预期行为


如果你想从可观察到的数据中得到最多一个项目,请使用。

谢谢你的完美答案,很难决定给谁“接受答案”。当你向我解释为什么我看到了错误以及我做错了什么时,塔马斯给了我解决问题的方法。