0

I need to pass city name in URL. I have declared Variable city and I am passing it in URL but it is not reading variable I have tried many methods but they are not working.

   const Weather=({city})=>{
  const [weather,setWeather]=useState('')
    useEffect(() => {
  
    axios
      .get(`http://api.weatherstack.com/current?access_key=58hgf1d8d36bf336f347d6cc9fbc7&query=${city}`)
      .then(response => {
        setWeather(response.data) 
      })
  }, []) 
// ...

2 Answers 2

2

You're using incorrect template literal syntax. You need:

.get(`http://api.weatherstack.com/current?access_key=Your_access_Key&query=${city}`)

But it also looks like you're missing an access key in this request.

Sign up to request clarification or add additional context in comments.

Comments

1

It should be ` instead of ' as follows:

axios.get(`http://api.weatherstack.com/current?access_key=Your_access_Key&query=${city}`)
    .then(response => {
      setWeather(response.data) 
    })

Hope this resolves the issue while passing dynamic city to the URL. Also make sure that you are passing the right access_key too.

5 Comments

.get(http://api.weatherstack.com/current?access_key=65f1d8d367f596bf336f347d6cfbc7&query=${city}) .then(response => { setWeather(response.data) }) }, []) It is also not working access key used is also correct.
I tried accessing the URL with Germany as the query param. It returns the error "invalid_access_key". Please have a look at it. Try generating a new access key & give it a try. That should resolve the issue.
Yeah, this works. here's the working URL for reference - api.weatherstack.com/…
Check whether you are getting the value for "city" properly from the parent component. If that's correct, this code should work.
there was error i was passing it as a capital . Thankyou for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.