Aak*_*kur 49 observable rxjs typescript angular
我试图map从服务电话,但得到一个错误.看看订阅没有在角度2中定义?它说,为了订阅我们需要从运营商内部返回.我也有回复陈述.
这是我的代码:
checkLogin(): Observable<boolean> {
return this.service.getData()
.map(
response => {
this.data = response;
this.checkservice = true;
return true;
},
error => {
// debugger;
this.router.navigate(['newpage']);
console.log(error);
return false;
}
)
.catch(e => {
return e;
});
}
Run Code Online (Sandbox Code Playgroud)
错误日志:
TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
Run Code Online (Sandbox Code Playgroud)
Ste*_*evy 18
在我的情况下,错误仅发生在e2e测试期间.它是由throwError我的AuthenticationInterceptor 引起的.
我从错误的源导入它,因为我使用了WebStorm的导入功能.我正在使用RxJS 6.2.
错误:
import { throwError } from 'rjxs/internal/observable/throwError';
Run Code Online (Sandbox Code Playgroud)
正确:
import { throwError } from 'rjxs';
Run Code Online (Sandbox Code Playgroud)
这里是拦截器的完整代码:
import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const reqWithCredentials = req.clone({withCredentials: true});
return next.handle(reqWithCredentials)
.pipe(
catchError(error => {
if (error.status === 401 || error.status === 403) {
// handle error
}
return throwError(error);
})
);
}
}
Run Code Online (Sandbox Code Playgroud)
sno*_*ete 17
在您的示例代码中,您的map运算符接收两个回调,只应该接收一个回调.您可以将错误处理代码移动到catch回调.
checkLogin():Observable<boolean>{
return this.service.getData()
.map(response => {
this.data = response;
this.checkservice=true;
return true;
})
.catch(error => {
this.router.navigate(['newpage']);
console.log(error);
return Observable.throw(error);
})
}
Run Code Online (Sandbox Code Playgroud)
您还需要导入catch和throw运算符.
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
Run Code Online (Sandbox Code Playgroud)
编辑:
请注意,通过Observable.throw在catch处理程序中返回,您实际上不会捕获错误 - 它仍然会显示在控制台上.
你正在返回一个Observable,你的代码只返回一个布尔值.所以你需要使用如下
.map(response => <boolean>response.json())
Run Code Online (Sandbox Code Playgroud)
如果您在使用其他常用服务checkservice,则可以使用
this.service.getData().subscribe(data=>console.log(data));
Run Code Online (Sandbox Code Playgroud)
这将使您的checkLogin()函数返回类型为void
checkLogin():void{
this.service.getData()
.map(response => {
this.data = response;
this.checkservice=true;
}).subscribe(data=>{ });
Run Code Online (Sandbox Code Playgroud)
你可以用它this.checkService来检查你的病情
当项目之间存在不同的 RxJS 版本时,我遇到了此错误。RxJS 中的内部检查失败,因为有几个不同的Symbol_observable. 最终,一旦从诸如 之类的展平运算符调用,该函数switchMap就会抛出异常。
尝试在某个入口点导入 symbol-observable。
// main index.ts
import 'symbol-observable';
Run Code Online (Sandbox Code Playgroud)
如果您的函数期望返回布尔值,请执行以下操作:
import { of, Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
checkLogin(): Observable<boolean> {
return this.service.getData()
.pipe(
map(response => {
this.data = response;
this.checkservice = true;
return true;
}),
catchError(error => {
this.router.navigate(['newpage']);
console.log(error);
return of(false);
})
)}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,是由于导入“takeUntil”的内部版本而不是运算符更改引起的
import { takeUntil } from 'rxjs/internal/operators/takeUntil';
Run Code Online (Sandbox Code Playgroud)
到
import { takeUntil } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
其他运营商也会发生这种情况
我忘记返回另一个 observable pipe(switchMap(
this.dataService.getPerson(personId).pipe(
switchMap(person => {
//this.dataService.getCompany(person.companyId); // return missing
return this.dataService.getCompany(person.companyId);
})
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108679 次 |
| 最近记录: |