Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

json - How to do easy forecast app using Javascript?

I was trying to do easy forecast app using javascript and API from https://openweathermap.org.

( I was watching ytb tutorial and doesn't work for me https://www.youtube.com/watch?v=GXrDEA3SIOQ&t=1s )

But everytime when I enter the city all that happens is the alert pops up saying “Wrong city name!”.

Do you have any idea how to fix it? Thank you

correct output is on the screen below enter image description here

var button= document.querySelector('.button');
var inputValue = document.querySelector('.inputValue');
var name = document.querySelector('.name');
var temp = document.querySelector('.temp');
var desc = document.querySelector('.desc');


button.addEventListener('click', function(name){
fetch('https://api.openweathermap.org/data/2.5/weather?q='+inputValue.value+'&appid=497df9aa02775d8c6cdec3c6d0cd159c')
    
.then(response => response.json())
.then(data => {
  var tempValue = data['main']['temp'];
  var nameValue = data['name'];
  var descValue = data['weather'][0]['description'];

  main.innerHTML = nameValue;
  desc.innerHTML = "Desc - "+descValue;
  temp.innerHTML = "Temp - "+tempValue;
  input.value ="";

})

.catch(err => alert("Wrong city name!"));
})
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>OpenWeatherMap Api</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  
    <div class="input">
      <input type="text" placeholder="Enter the city" class="inputValue">
      <input type="submit" value="Submit" class="button">
    </div>
 

  <div class="display">
    
      <h1 class="name" id="name"></h1>
      <p class="temp"></p> 
      <p class="desc"></p>
    
  </div>
<script src="app.js"></script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There were some major bugs in the code that I fixed and commented on.

  1. variable and class named name was in collision in few browsers so I've rennamed it to city
  2. Instead of .catch I've used try catch inside of the last then and I am logging the real error thrown by exception instead of "Wrong city name!" which is false
  3. variable main didn't exist
  4. some attributes of data might not be defined so I've used optional chaining ?.

Much more conditions needs to be treated (ex. negative response from server) but I hope it helps at the start.

var button= document.querySelector('.button');
var input = document.querySelector('.inputValue');
// rennamed variable `name` because it is reserved in JS
var city = document.querySelector('.city');
var temp = document.querySelector('.temp');
var desc = document.querySelector('.desc');

const baseUrl = "https://api.openweathermap.org/data/2.5/weather?q=";
const apiKey = "497df9aa02775d8c6cdec3c6d0cd159c";
const loadData=(event)=>{
  fetch(baseUrl+input.value+'&appid='+apiKey)
    .then(response => response.json())
    .then(data => {
        try {
          const tempValue = data?.main?.temp;
          const nameValue = data?.name;
          const descValue = data?.weather[0]?.description;
          
          // `main` was not defined. I've used `city`?
          city.innerHTML = nameValue;
          desc.innerHTML = "Desc - "+descValue;
          temp.innerHTML = "Temp - "+tempValue;
          input.value ="";
        } catch (err) {
           // hard coded error "Wrong city name!" was giving you falsy information
           // log the real error
           console.log(err);
        }
     });
}

button.addEventListener('click',loadData)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>OpenWeatherMap Api</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="input">
    <input type="text" placeholder="Zadejte město" class="inputValue">
    <input type="submit" value="Odeslat" class="button">
  </div>
  <div class="display">
      <h1 class="city"></h1>
      <p class="temp"></p> 
      <p class="desc"></p>
  </div>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...