[英]TypeError: Cannot read properties of undefined (reading 'country') Whenever I search for a city
我正在使用开放天气 map 和 Reactjs。
问题出现在我的WeatherContainer
组件中。
我希望我的搜索栏正常工作。 但是每当我搜索一个城市时,我都会收到此错误:
我尝试更改 API 密钥,但它什么也没做。
代码错误行指向:
我得到这样的数据:
WeatherContainer.tsx:
const [weather, setWeather] = useState({
city: "",
country: "",
temperature: 0,
description: "",
icon: "",
humidity: "",
feels: "",
visibility: "",
pressure: "",
longitude: "",
latitude: "",
windSpeed: "",
});
useEffect(() => {
if (fetchedData)
setWeather({
city: fetchedData.name,
country: fetchedData.sys.country,
temperature: Math.floor(fetchedData.main.temp - 273),
description: fetchedData.weather[0].description,
icon: `http://openweathermap.org/img/wn/${fetchedData.weather[0].icon}.png`,
humidity: fetchedData.main.humidity + "%",
feels: Math.floor(fetchedData.main.feels_like - 273) + "°C",
visibility: fetchedData.visibility + "m",
pressure: fetchedData.main.pressure + "hPa",
longitude: fetchedData.coord.lon,
latitude: fetchedData.coord.lat,
windSpeed: fetchedData.wind.speed + "m/s",
});
}, [fetchedData]);
编辑:
This is how I defined fetchedData
export const WeatherContainer = ({
fetchedData,
error,
}: {
fetchedData: any;
error: string;
}) => {
const [weather, setWeather] = useState({
city: "",
country: "",
temperature: 0,
description: "",
icon: "",
humidity: "",
feels: "",
visibility: "",
pressure: "",
longitude: "",
latitude: "",
windSpeed: "",
});
useEffect(() => {
if (fetchedData)
setWeather({
city: fetchedData.name,
country: fetchedData.sys.country,
temperature: Math.floor(fetchedData.main.temp - 273),
description: fetchedData.weather[0].description,
icon: `http://openweathermap.org/img/wn/${fetchedData.weather[0].icon}.png`,
humidity: fetchedData.main.humidity + "%",
feels: Math.floor(fetchedData.main.feels_like - 273) + "°C",
visibility: fetchedData.visibility + "m",
pressure: fetchedData.main.pressure + "hPa",
longitude: fetchedData.coord.lon,
latitude: fetchedData.coord.lat,
windSpeed: fetchedData.wind.speed + "m/s",
});
}, [fetchedData]);
我用这个 API https://openweathermap.org/current 。 我只是尝试遵循此文档,因此我将链接复制到了他们拥有的 API。
编辑#2
这是我建立连接的文件:
const API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
export const getWeatherCoordinates = async (
LAT: number,
LON: number
): Promise<any> => {
const API_URL = `https://api.openweathermap.org/data/2.5/weather?lat=${LAT}&lon=${LON}&appid=${API_KEY}`;
const respCoordinates = await fetch(API_URL);
const dataCoordinates = await respCoordinates.json();
return dataCoordinates;
};
export const getWeatherSearch = async (CITY: string): Promise<any> => {
const API_CITY = `https://api.openweathermap.org/data/2.5/weather?q=${CITY}&appid={API_KEY}`;
const respCity = await fetch(API_CITY);
const dataCity = await respCity.json();
return dataCity;
};
API 响应为:
{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}
根据您发布的错误图像和源代码,似乎在getWeatherSearch
中错过了{API_KEY}
前面的$
符号:
export const getWeatherSearch = async (CITY: string): Promise<any> => {
const API_CITY = `https://api.openweathermap.org/data/2.5/weather?q=${CITY}&appid=${API_KEY}`; // <- HERE
const respCity = await fetch(API_CITY);
const dataCity = await respCity.json();
return dataCity;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.