简体   繁体   English

当我尝试使用json获取API返回值时出错

[英]Error when I trying to get API return value using json

I have some problems with my code. 我的代码有问题。 I am using the open weather API to create this application. 我正在使用开放天气API创建此应用程序。 I can easily get the values in the weather.js file and moving to UI. 我可以轻松地在weather.js文件中获取值并移至UI。 Only it has some values that responds with error in the console. 只有它的某些值会在控制台中以错误响应。 Follows code and errors. 遵循代码和错误。

File weather.js 文件weather.js

class Weather {
  constructor(city, state) {
    this.apiKey = "xxxxxxxxxxxx";
    this.city = city;
    this.state = state;
  }

  // Fetch weather from API
  async getWeather() {
    const response = await fetch(
      `https://api.openweathermap.org/data/2.5/weather?q=${this.city},${this.state}&APPID=${this.apiKey}`
    );

    const responseData = await response.json();

    return responseData;
  }

  // Change weather location
  changeLocation(city) {
    this.city = city;
    this.state = state;
  }
}

File ui.js 文件ui.js

class UI {
  constructor() {
    this.location = document.getElementById("w-location");
    this.desc = document.getElementById("w-desc");
    this.string = document.getElementById("w-string");
    this.details = document.getElementById("w-details");
    this.icon = document.getElementById("w-icon");
    this.humidity = document.getElementById("w-humidity");
    this.feelsLike = document.getElementById("w-feels-like");
    this.dewpoint = document.getElementById("w-dewpoint");
    this.wind = document.getElementById("w-wind");
  }

  paint() {
    this.location.textContent = weather.name;
    this.desc.textContent = weather.weather[0].description;
  }
}

File app.js 档案app.js

// Init Weather Class
const weather = new Weather("rio de janeiro");

const ui = new UI();

// Get weather on DOM load
document.addEventListener("DOMContentLoaded", getWeather);

//weather.changeLocation("los angeles");

function getWeather() {
  weather
    .getWeather()
    .then(results => {
      ui.paint();
    })
    .catch(err => console.log(err));
}

Console Error: 控制台错误:

TypeError: Cannot read property '0' of undefined TypeError:无法读取未定义的属性“ 0”

at UI.paint (ui.js:16) 在UI.paint(ui.js:16)

at app.js:15 在app.js:15

In case if I make a console.log call in the weather.js file by the description property, it answers me correctly, but when I call in ui.js it gives this error. 如果我通过description属性在weather.js文件中进行console.log调用,它会正确回答我,但是当我在ui.js中调用时,会出现此错误。

And this is not just with this property, but with others as well, I tried to call the country property and says it is not set. 这不仅是与该财产有关,而且与其他财产一样,我试图称该国家财产为未设定。 How to solve? 怎么解决? Can someone help me? 有人能帮我吗?

You'd pass the results of the api call as a param to the paint method, since the Weather class doesn't have a weather property: 您将把api调用的结果作为参数传递给paint方法,因为Weather类没有weather属性:

  paint(data) {
    this.location.textContent = data.name;
    this.desc.textContent = data.weather[0].description;
  }


function getWeather() {
  weather
    .getWeather()
    .then(results => {
      ui.paint(results);
    })
    .catch(err => console.log(err));
}

Also make sure your api response actually has a weather prop, which is an array. 还要确保您的api响应实际上有一个weather道具,它是一个数组。

Follows corrected code: 遵循更正的代码:

app.js app.js

// Init Weather Class
const weather = new Weather("rio de janeiro");

const ui = new UI();

// Get weather on DOM load
document.addEventListener("DOMContentLoaded", getWeather);

//weather.changeLocation("los angeles");

function getWeather() {
  weather
    .getWeather()
    .then(results => {
      ui.paint(results);
    })
    .catch(err => console.log(err));
}

weather.js weather.js

class Weather {
  constructor(city, state) {
    this.apiKey = "17df2a26f3c8c6032f3eacdbbf3a8e25";
    this.city = city;
    this.state = state;
  }

  // Fetch weather from API
  async getWeather() {
    const response = await fetch(
      `https://api.openweathermap.org/data/2.5/weather?q=${this.city},${this.state}&APPID=${this.apiKey}`
    );

    const responseData = await response.json();

    console.log(responseData);

    return responseData;
  }

  // Change weather location
  changeLocation(city) {
    this.city = city;
    this.state = state;
  }
}

ui.js ui.js

class UI {
  constructor() {
    this.location = document.getElementById("w-location");
    this.desc = document.getElementById("w-desc");
    this.string = document.getElementById("w-string");
    this.details = document.getElementById("w-details");
    this.icon = document.getElementById("w-icon");
    this.humidity = document.getElementById("w-humidity");
    this.feelsLike = document.getElementById("w-feels-like");
    this.dewpoint = document.getElementById("w-dewpoint");
    this.wind = document.getElementById("w-wind");
  }

  paint(weather) {
    this.location.textContent = weather.name;
    this.desc.textContent = weather.weather[0].description;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM