Данные JSON Openweather API - PullRequest
       6

Данные JSON Openweather API

0 голосов
/ 26 февраля 2019

У меня проблемы с данными JSON, полученными из openweathermap.Возможно, я не вижу массив или что-то, но мне нужна помощь.

import React, { Component } from 'react';
import { connect } from 'react-redux';

class Weather extends Component {

render() {
    const weatherData = this.props.weather;
    console.log(weatherData);
    return (
        <div>
            <div>{weatherData.name}</div>
            <div>Temperature: {weatherData.main.temp}</div>
        </div>
    );
}
}

const mapStateToProps = (state) => {
    return {
        weather: state.weather
    };
};
export default connect(mapStateToProps)(Weather);

Ошибка в консоли:

TypeError: Cannot read property 'temp' of undefined

И этот объект получен из openweathermap API

    {
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 282.96,
    "pressure": 1030,
    "humidity": 71,
    "temp_min": 280.15,
    "temp_max": 286.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 3.1,
    "deg": 70
  },
  "clouds": {
    "all": 0
  },
  "dt": 1551209222,
  "sys": {
    "type": 1,
    "id": 1414,
    "message": 0.0082,
    "country": "GB",
    "sunrise": 1551163869,
    "sunset": 1551202579
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

Например, я получаю weatherData.name без проблемно weatherData.main.temp вызывает ошибку

console.log (weatherData) console.log(weatherData)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...