Angular 如何修复此问题(“this.http.post(…).map(…).concatMap不是函数”)?
升级我的angular项目后出现此错误 this.http.post(…).map(…).concatMap不是函数 这是导致错误的代码Angular 如何修复此问题(“this.http.post(…).map(…).concatMap不是函数”)?,angular,Angular,升级我的angular项目后出现此错误 this.http.post(…).map(…).concatMap不是函数 这是导致错误的代码 return this.http.post(apiURL, body, options) .map(response => response.json()) .concatMap(response => { // Check if the API_TOKEN is expired.
return this.http.post(apiURL, body, options)
.map(response => response.json())
.concatMap(response => {
// Check if the API_TOKEN is expired.
if (response.error.code == APIErrorCode.API_TOKEN_EXPIRED || response.error.code == 499) {
// Expired! Check max attempts.
if (attempts < MAX_REQUEST_ATTEMPT) {
console.log("API_TOKEN expired.");
return this.requestAPIToken().delay(500)
.concatMap(response => this.doPost(api, body, attempts + 1));
}
console.log("API_TOKEN expired, request attempt limit reached!");
}
return Observable.of(response);
})
.catch((err, caught) => {
// Check if the session authentication token is expired.
if (authToken && err.status == 403) {
try {
var response = JSON.parse(err._body);
if (response.error && response.error.code == APIErrorCode.SESSION_AUTH_TOKEN_EXPIRED) {
// Login session expired!
return Observable.of(response);
}
} catch (e) { }
}
// Check incidental bad request.
if (err.status == 400 && err.statusText == "Bad Request") {
try {
var response = JSON.parse(err._body);
if (response.error && response.error.code == 400 && response.error.message
&& [ "access denied", "bad" ].includes(response.error.message.toLowerCase())) {
// Check max attempts.
if (attempts < MAX_REQUEST_ATTEMPT) {
return this.requestAPIToken().delay(100)
.concatMap(response => this.doPost(api, body, attempts + 1));
}
console.log("Bad Request, request attempt limit reached!");
}
} catch (e) { }
}
return Observable.throw(err);
});
返回this.http.post(apirl、body、options)
.map(response=>response.json())
.concatMap(响应=>{
//检查API_令牌是否过期。
if(response.error.code==apierrocode.API_令牌_过期| | response.error.code==499){
//过期!检查最大尝试次数。
如果(尝试次数<最大请求次数<尝试次数){
log(“API_令牌已过期”);
返回此.requestAPIToken().delay(500)
.concatMap(response=>this.doPost(api,body,尝试次数+1));
}
log(“API_令牌已过期,已达到请求尝试限制!”);
}
可观察到的返回(响应);
})
.catch((错误,捕获)=>{
//检查会话身份验证令牌是否过期。
if(authToken&&err.status==403){
试一试{
var response=JSON.parse(err.\u body);
if(response.error&&response.error.code==apierrocode.SESSION\u AUTH\u TOKEN\u过期){
//登录会话已过期!
可观察到的返回(响应);
}
}捕获(e){}
}
//检查偶然的错误请求。
if(err.status==400&&err.statusText==Bad请求){
试一试{
var response=JSON.parse(err.\u body);
if(response.error&&response.error.code==400&&response.error.message
&&[“拒绝访问”,“错误”]。包括(response.error.message.toLowerCase()){
//检查最大尝试次数。
如果(尝试次数<最大请求次数<尝试次数){
返回此.requestAPIToken().delay(100)
.concatMap(response=>this.doPost(api,body,尝试次数+1));
}
log(“请求错误,已达到请求尝试限制!”);
}
}捕获(e){}
}
返回可观察。抛出(错误);
});
在我升级angular项目之前,它工作正常,我不知道现在该做什么如果您刚刚升级angular,您可能正在使用RxJS v6。如果是这样,您需要重构以使用可管道操作符 另外请注意,您应该使用
HttpClient
而不是Http
,后者现在已被长期弃用。然后,您还可以使用response.json()
删除第一个映射,因为HttpClient
会自动为您执行此操作
请记住,您需要重构代码中的所有可观察对象,不仅是来自HttpClient
的可观察对象,还有那些具有delay
操作符的可观察对象
请查看此处以了解更多信息:
从'rxjs'导入{of,throwError};
从“rxjs/operators”导入{concatMap,catchError,delay};
返回此.httpClient.post(APIRL、正文、选项)
.烟斗(
concatMap(响应=>{
//检查API_令牌是否过期。
if(response.error.code==apierrocode.API_令牌_过期| | response.error.code==499){
//过期!检查最大尝试次数。
如果(尝试次数<最大请求次数<尝试次数){
log(“API_令牌已过期”);
返回此.requestAPIToken().pipe(
延迟(500),
concatMap(response=>this.doPost(api、正文、尝试次数+1)
);
}
log(“API_令牌已过期,已达到请求尝试限制!”);
}
回复(回复);
}),
捕获错误((错误,捕获)=>{
//检查会话身份验证令牌是否过期。
if(authToken&&err.status==403){
试一试{
var response=JSON.parse(err.\u body);
if(response.error&&response.error.code==apierrocode.SESSION\u AUTH\u TOKEN\u过期){
//登录会话已过期!
回复(回复);
}
}捕获(e){}
}
//检查偶然的错误请求。
if(err.status==400&&err.statusText==Bad请求){
试一试{
var response=JSON.parse(err.\u body);
if(response.error&&response.error.code==400&&response.error.message
&&[“拒绝访问”,“错误”]。包括(response.error.message.toLowerCase()){
//检查最大尝试次数。
如果(尝试次数<最大请求次数<尝试次数){
返回此.requestAPIToken().pipe(
延迟(100),
concatMap(response=>this.doPost(api、正文、尝试次数+1)
);
}
log(“请求错误,已达到请求尝试限制!”);
}
}捕获(e){}
}
回程抛掷器(err);
})
);
如果您使用的是v5.5,那么我的回答也会有所帮助,但您的问题在于缺少准确的
concatMap
操作符导入。请重构到可管道操作符,或者只添加import'rxjs/add/operator/concatMap';
此导入将使用concatMap
操作符对Observable
对象进行猴子补丁-默认情况下不存在该对象
有关在RxJS v5中导入运算符的更多信息,请参见:
如果您刚刚升级了angular,您可能正在使用
import { of, throwError } from 'rxjs';
import { concatMap, catchError, delay } from 'rxjs/operators';
return this.httpClient.post(apiURL, body, options)
.pipe(
concatMap(response => {
// Check if the API_TOKEN is expired.
if (response.error.code == APIErrorCode.API_TOKEN_EXPIRED || response.error.code == 499) {
// Expired! Check max attempts.
if (attempts < MAX_REQUEST_ATTEMPT) {
console.log("API_TOKEN expired.");
return this.requestAPIToken().pipe(
delay(500),
concatMap(response => this.doPost(api, body, attempts + 1)
);
}
console.log("API_TOKEN expired, request attempt limit reached!");
}
return of(response);
}),
catchError((err, caught) => {
// Check if the session authentication token is expired.
if (authToken && err.status == 403) {
try {
var response = JSON.parse(err._body);
if (response.error && response.error.code == APIErrorCode.SESSION_AUTH_TOKEN_EXPIRED) {
// Login session expired!
return of(response);
}
} catch (e) { }
}
// Check incidental bad request.
if (err.status == 400 && err.statusText == "Bad Request") {
try {
var response = JSON.parse(err._body);
if (response.error && response.error.code == 400 && response.error.message
&& [ "access denied", "bad" ].includes(response.error.message.toLowerCase())) {
// Check max attempts.
if (attempts < MAX_REQUEST_ATTEMPT) {
return this.requestAPIToken().pipe(
delay(100),
concatMap(response => this.doPost(api, body, attempts + 1)
);
}
console.log("Bad Request, request attempt limit reached!");
}
} catch (e) { }
}
return throwError(err);
})
);
return this.http.post(apiURL, body, options).pipe(
map(response => response.json()),
concatMap(response => {
//REST OF THE CODE
return of(response);
}))
return this.http.post(...).pipe(
map(...).reduce(...)
}))