我正在尝试访问openweathermaps API以获取某个位置的风速。但它每次都会给我带来一个错误。
未处理的拒绝(TypeError(:无法读取的属性"speed"未定义
这是我的位置类(或我称之为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 STACKOVERFLOW';
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中得到了speed属性吗如果你不介意的话,也可以发送从你的api中获得的res
。