By Subhajit


2019-07-11 18:59:52 8 Comments

Solved it! Here is my code after adding curly brackets and removing the ";" from first .then:

const api = `https://api.darksky.net/forecast/68568119ac38ef741a51540efb8ad8b3/${lat},${long}`;
fetch(api)
.then(data =>{
  return data.json();
})
.then(data =>{
  console.log(data);
});     

1 comments

@k3llydev 2019-07-11 19:19:25

You got a bunch of errors in your code. First of all, you added an unnecessary semicolon at your .then() promise.

fetch(api)
  .then(data =>{
    return data.json();
  }); // UNNECESSARY SEMICOLON
  .then(data =>{
    console.log(data);
});

Also, you are not giving your string the correct values due to the single quotation mark and wrong syntax:

const api = `api.openweathermap.org/data/2.5/weather?${lat},${lon}`;
//          ^--GRAVE ACCENT    USE CURLY BRACES ------^

And finally, I bet api.openweathermap.org is not a directory in your server. Use // at the beginning of your URL so JavaScript can understand that you are giving the full URL:

const api = `//api.openweathermap.org/data/2.5/weather?${lat},${lon}`;
//   HERE ---^

With everything explained, here is the result of your code but working:

function getWeather(lat, lon) {
  const api = `//api.openweathermap.org/data/2.5/weather?${lat},${lon}`;
  fetch(api)
    .then(data => {
      return data.json();
    })
  .then(data => {
    console.log(data);
  });
}

getWeather(0,0);

@FZs 2019-07-11 19:39:36

The ` character is called grave accent, not tilde. Tilde is ~

Related Questions

Sponsored Content

55 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

35 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

61 Answered Questions

[SOLVED] How to check whether a checkbox is checked in jQuery?

56 Answered Questions

[SOLVED] How to replace all occurrences of a string?

57 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

3 Answered Questions

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

79 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5841341 View
  • 7285 Score
  • 79 Answer
  • Tags:   javascript arrays

38 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content