Javascript 为什么我能';t访问react中的数组列表
我获取一些日期并获取数组,我可以console.log并查看所有人员,但当我尝试映射或访问数组中的某些元素时,我得到了未定义的错误 还有这个 未处理的拒绝(TypeError):无法读取未定义的属性“map”Javascript 为什么我能';t访问react中的数组列表,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我获取一些日期并获取数组,我可以console.log并查看所有人员,但当我尝试映射或访问数组中的某些元素时,我得到了未定义的错误 还有这个 未处理的拒绝(TypeError):无法读取未定义的属性“map” const [key, setKey] = useState(false); const [country, setCountry] = useState([]); const [nextFive, setFive] = useState([]); useEffect((
const [key, setKey] = useState(false);
const [country, setCountry] = useState([]);
const [nextFive, setFive] = useState([]);
useEffect(() => {
getWeather();
}, [key]);
const getCity = async (e) => {
e.preventDefault();
const cityName = e.target.elements.cityName.value;
const api_call = await fetch(`http://dataservice.accuweather.com/locations/v1/cities/search?apikey=${process.env.REACT_APP_WEATHER_API_KEY}&q=${cityName}&details=true`);
const data = await api_call.json();
setKey(data[0].Key);
setCountry(data[0]);
return data[0].Key;
}
const getWeather = async (cityKey = key) => {
const proxy = `https://cors-anywhere.herokuapp.com/`;
const link = `${proxy}http://dataservice.accuweather.com/forecasts/v1/daily/5day/${cityKey}?apikey=${process.env.REACT_APP_WEATHER_API_KEY}&details=true&metric=true`;
const api_call = await fetch(link);
const data = await api_call.json();
setFive(data.DailyForecasts);
};
console.log(nextFive); if i tray console.log(nextFive[0].something) i will get error :)
console.log(country);
{
nextFive.map((item) => {
return <p>{item.Date}</p>
})
}
const[key,setKey]=useState(false);
const[country,setCountry]=useState([]);
const[nextFive,setFive]=useState([]);
useffect(()=>{
getWeather();
},[关键];
const getCity=async(e)=>{
e、 预防默认值();
const cityName=e.target.elements.cityName.value;
const api_call=等待获取(`http://dataservice.accuweather.com/locations/v1/cities/search?apikey=${process.env.REACT\u APP\u WEATHER\u API\u KEY}&q=${cityName}&details=true`);
const data=wait api_call.json();
设置键(数据[0]。键);
setCountry(数据[0]);
返回数据[0]。键;
}
const getWeather=async(cityKey=key)=>{
常量代理=`https://cors-anywhere.herokuapp.com/`;
常量链接=`${proxy}http://dataservice.accuweather.com/forecasts/v1/daily/5day/${cityKey}?apikey=${process.env.REACT\u APP\u WEATHER\u API\u KEY}&details=true&metric=true`;
const api_call=等待获取(链接);
const data=wait api_call.json();
SET5(数据、每日预测);
};
console.log(nextFive);如果我使用托盘控制台.log(nextFive[0].something),我将得到错误:)
控制台日志(国家);
{
nextFive.map((项目)=>{
返回{item.Date}
})
}
并获得此未经处理的拒绝(TypeError):无法读取未定义的属性“map”发生这种情况是因为您试图显示数组中尚未填充的项的第一个元素
const [key, setKey] = useState(false);
const [country, setCountry] = useState([]);
const [nextFive, setFive] = useState([]);
useEffect(() => {
getWeather();
}, [key]);
const getCity = async (e) => {
e.preventDefault();
const cityName = e.target.elements.cityName.value;
const api_call = await fetch(`http://dataservice.accuweather.com/locations/v1/cities/search?apikey=${process.env.REACT_APP_WEATHER_API_KEY}&q=${cityName}&details=true`);
const data = await api_call.json();
setKey(data[0].Key);
setCountry(data[0]);
return data[0].Key;
}
const getWeather = async (cityKey = key) => {
const proxy = `https://cors-anywhere.herokuapp.com/`;
const link = `${proxy}http://dataservice.accuweather.com/forecasts/v1/daily/5day/${cityKey}?apikey=${process.env.REACT_APP_WEATHER_API_KEY}&details=true&metric=true`;
const api_call = await fetch(link);
const data = await api_call.json();
setFive(data.DailyForecasts);
};
console.log(nextFive); if i tray console.log(nextFive[0].something) i will get error :)
console.log(country);
{
nextFive.map((item) => {
return <p>{item.Date}</p>
})
}
您可以调用额外的钩子,例如weatherLoading,一旦api调用成功,您就可以将weatherLoading的状态设置为true,然后简单地执行以下操作:
weatherLoading && console.log(nextFive[0].something)
发生这种情况是因为您试图显示数组中尚未填充的第一个元素 您可以调用额外的钩子,例如weatherLoading,一旦api调用成功,您就可以将weatherLoading的状态设置为true,然后简单地执行以下操作:
weatherLoading && console.log(nextFive[0].something)
如果您提供codesandbox、JSFIDLE或simialrit等代码演示,这将非常有用。如果您提供codesandbox、JSFIDLE或simialr等代码演示,这将非常有用