我打電話給openweathermap api來檢索higher-order
組件中的天氣,然後渲染'Main'組件。但是,Ajax調用成功後,我收到以下錯誤:Promise中未定義的錯誤實現
TypeError: _getWeather2.default(...) is undefined
代碼:
MainContainer.js:
import React from "react";
import getWeather from "../action/getWeather";
import Main from "./Main";
const MainContainer =() => {
// the error is somewhere related to this component
var weather = getWeather("london", "uk")
.then((data) => {
console.log("data in maincontainer is...", data);
return <Main />;
});
}
export default MainContainer;
getWeather.js:
const getWeather = (city, country) => {
let queryPath = `http://api.openweathermap.org/data/2.5/forecast?q=${ city },${ country }&APPID=${ inserytKey }&mode=json`
console.log("queryPath is...", queryPath);
fetch(queryPath)
.then((response) => response.json())
.then((data) => {
console.log("data is...", data);
return data;
})
.catch((err) => {
console.log(err);
})
};
export default getWeather;
我在做什麼錯了嗎?
的GetWeather沒有返回一個承諾 –