Javascript 在redux thunk中链接操作的正确方式?
我第一次使用redux thunk。链接操作的正确方式是什么 我想在用户输入后获取位置,当有来自GoogleMapsAPI的数据响应时,我想立即使用该数据获取该位置的天气。 Redux thunk正在工作,但仅适用于获取位置的第一个操作。 request2中的Data2总是未定义的,你能告诉我为什么吗Javascript 在redux thunk中链接操作的正确方式?,javascript,reactjs,redux,react-redux,redux-thunk,Javascript,Reactjs,Redux,React Redux,Redux Thunk,我第一次使用redux thunk。链接操作的正确方式是什么 我想在用户输入后获取位置,当有来自GoogleMapsAPI的数据响应时,我想立即使用该数据获取该位置的天气。 Redux thunk正在工作,但仅适用于获取位置的第一个操作。 request2中的Data2总是未定义的,你能告诉我为什么吗 export function fetchLocation(city) { const urlGoogle = `https://maps.googleapis.com/maps/a
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 });
});
});
};
}
第二个请求很可能没有返回名为response.data2的字段,因此当您对其进行分解时,data2将是未定义的。您可能仍然需要查找名为data的字段,但给它一个不同的本地参数名,如:request2.then{data:data2}