我从我的API中获取了一些数据,但是当它返回时,getLocation()
的then
部分不被执行,因此未调用调度,而我的操作RECIEVE_SHOPS
ISN'T触发。
我在做什么错?
export function receiveShops(data) {
return { type: 'RECIEVE_SHOPS', shops: data.shops, time: Date.now() }
}
// http://localhost:3001/nearby?lat=30&long=-6
export function fetchShops(location) {
return function (dispatch) {
dispatch(requestShops());
console.log("--fetchShops--");
console.log(location)
return axios({
method: 'get',
url: 'http://localhost:3001/nearby',
params: {
lat: location.lat,
long: location.long
}
})
}
}
function getLocation() {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(pos => {
resolve(pos);
});
} else {
reject("Geolocation is not supported by this browser.")
}
}
)
}
export function loadNearbyShops() {
return function (dispatch) {
return getLocation()
.then(position => dispatch(getCoords(position)))
.then(position => dispatch(fetchShops(position)))
.then(data => dispatch(receiveShops(data)))
.catch(error => console.log(error))
};
}
export function getCoords(position) {
return {
type: 'GET_LOCATION',
long: position.coords.longitude,
lat: position.coords.latitude
}
}
编辑:
这就是我称loadNearbyShops
的方式:
store.dispatch(loadNearbyShops())
阅读咖喱是什么。在您的功能中,您正在返回一个函数,这意味着您必须两次调用该功能才能使用实际内容运行内部功能。
function curriedSum(a){
return function(b) {
console.log(a + b)
}
}
curriedSum(10)(5)
您的外部函数不采用任何参数,但是您的内心函数会做出任何参数。您正在传达对dispatch
在内部功能中应具有的定义。您正在寻找更多的内容:
loadNearbyShops()(store.dispatch)