1

I'm new to react development. I tried to create a simple project which searches for the weather of a city by using an API. Using fetch I tried to call the API:

getWeather = async (e) => {
    e.preventDefault();
    const url = 'https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${API_KEY}';
    const api_call = await fetch(url);
    const data = await api_call.json() ;
    console.log(data);
}

But when I submit, the console log says wrong API KEY. Instead if I give the API key directly into the URL it works, can somebody help me?

4
  • 2
    Use template string. You set just a string ${API_KEY} it needs to be a variable. const url = `api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${API_KEY}`; Commented Nov 23, 2019 at 17:18
  • Where is API_KEY being set? Commented Nov 23, 2019 at 17:18
  • This is what Alexandr means: use like this const url = https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${API_KEY}; Commented Nov 23, 2019 at 17:21
  • You can use backticks instead of a single quote. https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${API_KEY} Commented Nov 23, 2019 at 17:22

1 Answer 1

2

the problem is that you're using the incorrect type of quotes. you're using ' (single-quote or apostrophe) and you need to be using backticks, it's the key to the left of the 1 key on most keyboards if you want to use string interpolation (that's what the ${} is called)

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

1 Comment

Glad to be of service! If the solution worked for you please mark it as accepted so that when others find this question they can easily see the solution

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.