svmon
svmon

Reputation: 19

Angular 2: HTTP GET Request with ID

I have a http get request for a specific object. Therefore, I pass the ID in the request. On the serverside the value is now ${id} but not the actual passed number. What am I doing wrong? In another app it's working..

Service

getLeague(id){
    return this.http.get('api/leagues/${id}').map(res => res.json());
};

Server-side

router.route('/:league_id')
     .get(function(req,res){
         League.findOne({id: req.params.league_id})
     .exec(function(err,docs){
         if(err)
         res.send(err);
     res.json(docs);
  })});

In my test code I just simply call, e.g. getLeague(5), however, I receive the error (seems like ID ist not holding the actual value but just the variable name):

message: "Cast to number failed for value "${id}" at path "id" for model "League"", name: "CastError", stringValue: ""${id}"

Thanks!

Upvotes: 0

Views: 16083

Answers (3)

Aravind
Aravind

Reputation: 41543

As said in the syntax in this URL

`api/leagues/${id}`

Using back tick ``` should fix this

Upvotes: 0

A. Tim
A. Tim

Reputation: 3196

You do not pass {{id}} to your link yet. Your request to backend includes "/${id}" part. To resolve variable in string in Typescript you should wrap your string with ticks to utilize interpolation, i.e.

return this.http.get(`api/leagues/${id}`).map(res => res.json());

Upvotes: 2

Kirk Larkin
Kirk Larkin

Reputation: 93043

You're not using string interpolation (Template Literals). Instead of single quotes, you need back ticks around your URL.

Upvotes: 3

Related Questions