解析 Openweathermap 请求,未处理的拒绝 (TypeError):无法读取未定义的 属性 'speed'

Parsing Openweathermap request, Unhandled Rejection (TypeError): Cannot read property 'speed' of undefined

我正在尝试访问 openweathermaps API 以获取特定位置的风速。但它每次都会给我一个错误。

Unhandled Rejection (TypeError): Cannot read property 'speed' of undefined

这是我的位置 class(或者我称之为 Spot):

import React, { Component, setState } from 'react'
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
import L from 'leaflet';
import axios from 'axios';

var owmApiKey = 'HIDDEN FROM Whosebug';

var myIcon = L.icon({ //SETS UP THE PIN ICON THAT IS USED TO PLOT MARKERS ON THE MAP
    iconUrl: 'https://cdn0.iconfinder.com/data/icons/small-n-flat/24/678111-map-marker-512.png',
    iconSize: [41,41],
    iconAnchor: [12.5,41],
    popupAnchor: [0, -41]
});

export default class Spot extends Component {

    constructor(props) {
        super()
        this.state = {
            county_name: props.county_name,
            latitude: props.latitude,
            longitude: props.longitude,
            spot_id: props.spot_id,
            spot_name: props.spot_name,
            wind_speed: 0,
        }
    }

    getWindSpeed = (lon, lat) => {//THE FUNCTION TO POPULATE THE LIST OF SPOTS USING AXIOS
            fetch(`api.openweathermap.org/data/2.5/weather?lat=${this.state.latitude}&lon=${this.state.longitude}&appid=${owmApiKey}`)
            .then(res => {
                this.setState({
                    wind_speed: res.wind.speed
                });
            });
    }

    componentDidMount() {
        this.getWindSpeed();
    }

    render() {
        return(
        <Marker position={[this.state.latitude,this.state.longitude]} icon={myIcon}>
            <Popup>
                {this.state.spot_name + ", " + this.state.county_name + " Wind Speed: " + this.state.wind_speed}
            </Popup>
        </Marker>    
        )
    }

}

您确定在 res.wind 中获得速度 属性 如果您不介意,也可以发送您从 api.

获得的 res