arevilla009
arevilla009

Reputation: 453

Problems sending parameters from Angular to Node

I'm developing an easy website using Angular and nodeJS. I'm trying to send one parameter "email" from an Angular service when accessing a route defined in node server. The Angular service function:

enter image description here

The node defined accessing route: enter image description here

The configuration of the node server:

enter image description here

I tried using bodyParser and using req.body for getting the email parameter sent from the frontend, but it seems to be lost. It's getting an empty dictionary when req.params or req.body are printed. enter image description here

Someone knows what can be happening? Thanks for reading!

Upvotes: 2

Views: 503

Answers (1)

eol
eol

Reputation: 24555

From the looks of it you want to read the path-param from the request url (see route-params under https://expressjs.com/en/guide/routing.html).

On the nodejs side, you need to change your handler to:

router.get('/profile/:email', ...) => {
   const email = req.params.email;
});

On the client-side, you need to correctly add the email path-segment. One way to do this is:

this.http.get<any>(`${this.URL}/profile/` + encodeURIComponent(email));

Instead of path-parameters you can also do this is using query-parameters. You'd need the following changes:

nodejs:

router.get('/profile', ...) => {
   const email = req.query.email;
});

angular:

this.http.get<any>(`${this.URL}/profile`, { params: { email } });

Upvotes: 2

Related Questions