rollback
rollback

Reputation: 111

Why I can't fetch data properly with proxy?

I want to fetch data from an API for my weather app. Here is my code example:

if(navigator.geolocation){
  navigator.geolocation.getCurrentPosition(position =>{
    long = position.coords.longitude;  
    lat = position.coords.latitude;
    const proxy = "https://cors-anywhere.herokuapp.com/";
    const api = 
    '${proxy}https://api.darksky.net/forecast/4f94f918cc790acff94560f7961cc71f/${lat},${long}';
    fetch(api)
    .then(response =>{
        return response.json();
    })
    .then(data =>{
      console.log(data)
      const {temperature, summary} = data.currently;
      temperatureDegree.textContent = temperature
    });
  });
}

And it doesn't fetch. I don't really know what's going on. Weather API from Darksky.

Upvotes: 2

Views: 441

Answers (1)

Paul Fitzgerald
Paul Fitzgerald

Reputation: 12129

You are wanting to use template literals.

You need to use backticks instead of '.

This line needs to be:

const api = `${proxy}https://api.darksky.net/forecast/4f94f918cc790acff94560f7961cc71f/${lat},${long}`;

Upvotes: 3

Related Questions