[英]Angular - receive and return Observable<T> response in Http.post
I implemented an method which returns an Observable.我实现了一个返回 Observable 的方法。 Inside this method I am using http.post to send a request to the backend.
在此方法中,我使用 http.post 向后端发送请求。 After receiving the response, which is a JSON object, I want to store this in an Observable variable and return that variable.
收到响应后,这是一个 JSON 对象,我想将其存储在一个 Observable 变量中并返回该变量。 But somehow I didn't manage to solve that problem.
但不知何故我没能解决这个问题。 In .subscribe the res variable is not stored in the postResponse variable, but I can see in the "local" console.log that the res variable has the correct value.
在 .subscribe 中, res 变量未存储在 postResponse 变量中,但我可以在“本地” console.log 中看到 res 变量具有正确的值。 The global console.log is empty.
全局 console.log 是空的。 Furthermore I get the error:
此外,我收到错误:
"TS2322: Type 'ArqResponse' is not assignable to type 'Observable'" error for the return.
“TS2322:类型‘ArqResponse’不可分配给类型‘Observable’”返回错误。
My code looks like this:我的代码如下所示:
postARQRequest(request): Observable<ArqResponse>{
let postResponse = new ArqResponse;
const result = this.http.post<ArqResponse>(this.arqUrl, request)
.subscribe((res: ArqResponse) => { postResponse = res; console.log('shadow: ' + res)});
console.log('global: ' + JSON.stringify(postResponse));
return postResponse;
}
My questions are:我的问题是:
.subscribe
seems to be wrong since I get: .subscribe
似乎是错误的,因为我得到:this.arqService.postARQRequest(...).subscribe is not a function error
this.arqService.postARQRequest(...).subscribe 不是函数错误
I'm guessing this is what you want:我猜这就是你想要的:
postARQRequest(request): Observable<ArqResponse>{
return this.http.post<ArqResponse>(this.arqUrl, request);
}
There's no need to subscribe to anything here.无需在这里订阅任何内容。 Given that
this.http.post
returns the type you want, just return that.鉴于
this.http.post
返回您想要的类型,只需返回即可。
If you really want to store the response in a local variable, there are some ways to do that:如果您真的想将响应存储在局部变量中,有一些方法可以做到这一点:
Use a promise instead, for getting the result.改用承诺,以获得结果。 Make it observable using
of
later on:使其可观察使用
of
后面:
async postARQRequest(request): Observable<ArqResponse>{
let postResponse = new ArqResponse;
postResponse = await this.http.post<ArqResponse>(this.arqUrl, request).toPromise();
return of(postResponse);
}
Use the tap
operator to react to the response, but not mutate it使用
tap
运算符对响应做出反应,但不对其进行变异
postARQRequest(request): Observable<ArqResponse>{
return this.http.post<ArqResponse>(this.arqUrl, request).pipe(
tap((res) => ...) // do stuff with res here, but it won't be mutated
);
}
Use the map
operator to map the response to something else使用
map
运算符将响应映射到其他内容
postARQRequest(request): Observable<ArqResponse>{
return this.http.post<ArqResponse>(this.arqUrl, request).pipe(
map((res) => ...) // do stuff with res here, but it *will map to whatever you return from this handler*
);
}
I think this may help you我想这可能对你有帮助
postARQRequest(): Observable<ArqResponse[]> {
return this.http.post(this.arqUrl, request)
.map(this.extractData()) <== passing result of function
.catch(this.handleError()); <== passing result of function
}
handle response and error here在这里处理响应和错误
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
let errMsg = error.message || 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
YThe method http.post
already return an Observable
, so you can directly do like that: YThe方法
http.post
已经返回一个Observable
,这样你就可以直接做这样的:
postARQRequest(request): Observable<ArqResponse>{
return this.http.post<ArqResponse>(this.arqUrl, request);
}
and then subscribe to it: this.arqService.postARQRequest(...).subscribe()
然后订阅它:
this.arqService.postARQRequest(...).subscribe()
If you want to transform an object to an Observable, you can use of
from Rxjs6 :如果你想一个对象转换为可观察到的,你可以使用
of
从Rxjs6:
import { of } from 'rxjs';
// ...
// create an Observable from a value:
of(someObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.