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
  • 520
  • 2
  • 7
  • 20

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
  • 37,143
  • 5
  • 39
  • 60
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
  • 35,834
  • 5
  • 42
  • 73