0
我第一次使用redux thunk
。鏈接操作的正確方式是什麼?在redux thunk中鏈接操作的正確方法?
我想獲取用戶輸入後給出的位置,當有響應數據從Google Maps API
,那麼我想立即使用該數據來獲取該位置的天氣。 Redux thunk
正在工作,但僅用於第一次操作(取回位置)。 Data2
在request2
總是undefined
,你能告訴我這是爲什麼嗎?
export function fetchLocation(city) {
const urlGoogle = `https://maps.googleapis.com/maps/api/geocode/json?address=${city}&key=${API_KEY_GOOGLE}`;
const request = axios.get(urlGoogle);
return (dispatch) => {
request.then(({ data }) => {
dispatch({ type: FETCH_LOCATION, payload: data });
const lat = data.results["0"].geometry.location.lat;
const lng = data.results["0"].geometry.location.lng;
const urlWunder = `https://api.wunderground.com/api/${API_KEY_WUNDERGROUND}/forecast10day/q/${lat},${lng}.json`;
console.log(urlWunder); // Link is ok, it works in browser
const request2 = axios.get(urlWunder);
request2.then(({ data2 }) => {
console.log('Data2', data2); // Getting undefined, why ?
dispatch({ type: FETCH_WEATHER, payload: data2 });
});
});
};
}
非常感謝你@markerikson! –