Store result of query in local variable and send a confirmation for the same in Angular

Multi tool use


Store result of query in local variable and send a confirmation for the same in Angular
My node server is giving me a response for a query result which I want to store in my Angular Service's Local Variable(movie) and pass a confirmation message({"result":true}) to the asking angular component.
redirect(id){
this.movie.getMovie(id).subscribe( confirmation =>{
if(confirmation.result) this.router.navigate(["/movies/movDetails"]);
});
}
This is my angular Component
getMovie(id):Observable<any>{
return this.http.post("http://localhost:3000/getMovie",{ "_id":id }).subscribe(IncomingValue => this.movie = IncomingValue).pipe(map( return {"result":true} ));
}
Service Component
i know so but post is comparatively more secure cause eventually i will be encrypting and then sending my data
– Karan Gaur
yesterday
1 Answer
1
When retrieving data, one often uses a get
, not a post
. This is what one of my simple gets looks like:
get
post
getProducts(): Observable<IProduct> {
return this.http.get<IProduct>(this.productUrl);
}
Using your code ... you can then use RxJS pipeable operators to perform additional operations:
getMovie(id):Observable<any>{
return this.http.post("http://localhost:3000/getMovie",{ "_id":id })
.pipe(
tap(IncomingValue => this.movie = IncomingValue),
map(() => {return {"result":true}} )
);
}
The first pipeable operator, tap, stores the incoming value in your local property.
The second pipeable operator, map, maps the result as the defined key and value pair.
Hope this helps.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The service should not be subscribing. The subscribing belongs in the component.
– DeborahK
yesterday