[英]Proper way of chaining operations in redux thunk?
I'm using redux thunk
first time. 我第一次使用
redux thunk
。 What is proper way of chaining operations? 链接操作的正确方法是什么?
I want to fetch location after user input is given and when there is response with data from Google Maps API
, then I want to immediately use that data to fetch weather for that location. 我想在给出用户输入后获取位置,并且当
Google Maps API
数据响应时,我想立即使用该数据来获取该位置的天气。 Redux thunk
is working, but only for first operation (fetching location). Redux thunk
正在运行,但仅适用于首次操作(获取位置)。 Data2
in request2
is always undefined
, can you tell me why is that? request2
Data2
始终是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 });
});
});
};
}
It's likely that the second request isn't returning a field named, say, response.data2
, so when you destructure it, data2
will be undefined. 第二个请求很可能没有返回一个名为
response.data2
的字段,因此当您对其进行data2
时, data2
将是未定义的。 You probably still need to look for a field named data
, but give it a different local parameter name, like: request2.then({data : data2})
. 您可能仍需要查找一个名为
data
的字段,但为它指定一个不同的本地参数名称,例如: request2.then({data : data2})
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.