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

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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





The service should not be subscribing. The subscribing belongs in the component.
– DeborahK
yesterday





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.

lexbeNKSyqi37Le1UKXcad N,mI6 IaCtpc8,zHROuz
6CSzm1MOWNdwYJQKspCoI

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template