Reputation: 499
I am creating url by concatenating constant string and state variables as follows :
var url={"http://localhost:3000/get-players/"+{this.state.city}+"/"+{this.state.sport}};
But I am getting error. Can someone tell me where am I making mistake.
Upvotes: 0
Views: 10712
Reputation: 2170
var url={"http://localhost:3000/get-players/"+{this.state.city}+"/"+{this.state.sport}};
{this.state.city}
- is not string.
1: Correct variant for ES5:
var url="http://localhost:3000/get-players/" + this.state.city + "/" + this.state.sport;
this.state.city
and this.state.sport
should be declared and should be a type of string
.
2: Correct variant for ES6
var url=`http://localhost:3000/get-players/${this.state.city}/${this.state.sport}`;
Upvotes: 0
Reputation: 4008
If you don't want to use ES6 templating, do it the old way but remove the extra brackets ({}
) around the variables.
var url = "http://localhost:3000/get-players/" + this.state.city + "/" + this.state.sport;
Upvotes: 1
Reputation: 3662
You're string concatenation is incorrect, you could use string interpolation like this:
var url = `http://localhost:3000/get-players/${this.state.city}/${this.state.sport}`
Upvotes: 2