0
axios.get('http://192.168.0.103:3000/weather/Hourly?longitude=${coords.longitude}&latitude=${coords.latitude}')
.then(result => {
  console.log(result);
  dispatch(fetchWeatherDailySucceeded(result.data.weatherInfor))
})
.catch(error => {
  dispatch(fetchWeatherDailyFailed());
})

Somehow the string doesn't recognize my injection and the url send to the server is the whole string with ${}

enter image description here

Huy Nguyen
  • 391
  • 1
  • 5
  • 16

2 Answers2

1

${} works with `` backticks not with " or '

You need to use `` ( backticks)

`http://192.168.0.103:3000/weather/Hourly?longitude=${coords.longitude}&latitude=${coords.latitude}`

Or if you use ' or " you can use string concatanation

'http://192.168.0.103:3000/weather/Hourly?longitude=' coords.longitude + '&latitude=' + coords.latitude 
Code Maniac
  • 35,187
  • 4
  • 31
  • 54
1

You cannot inject variables inside '' or "". You should ``.MDN says

Template literals are enclosed by the back-tick (``)

axios.get(`http://192.168.0.103:3000/weather/Hourly?longitude=${coords.longitude}&latitude=${coords.latitude}`)
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62