Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React/Redux:使用承诺获取用户地理位置,并将位置发送给动作创建者_Javascript_Node.js_Reactjs_Redux - Fatal编程技术网

Javascript React/Redux:使用承诺获取用户地理位置,并将位置发送给动作创建者

Javascript React/Redux:使用承诺获取用户地理位置,并将位置发送给动作创建者,javascript,node.js,reactjs,redux,Javascript,Node.js,Reactjs,Redux,在我的store.js中,我试图从浏览器中获取最终用户的地理位置,然后发送一个动作创建者,但这没有发生 function showPosition(position) { console.log("showPosition"); //console.log(position); return position; } function getLocation() { // Get the current 'global' time from an API usin

在我的
store.js
中,我试图从浏览器中获取最终用户的地理位置,然后发送一个动作创建者,但这没有发生

function showPosition(position) {
    console.log("showPosition");
    //console.log(position);
    return position;
}

function getLocation() {
    // Get the current 'global' time from an API using Promise
    console.log("getLocation")
    return new Promise((resolve, reject) => {
        if (navigator.geolocation) {
            console.log("Boolean navigator.getLocation:" + Boolean(navigator.geolocation))
            Promise.resolve(navigator.geolocation.getCurrentPosition(showPosition))
        } else {
            Promise.reject("Geolocation is not supported by this browser.")
        }
    })
}

function loadNearbyShops() {
    return function (dispatch) {
        return getLocation().then(
            position => dispatch(getCoords(position))
        ).catch(
            error => console.log(error)
            );
    };
}

store.dispatch(loadNearbyShops())
但这不会触发我下面的动作

另一方面,我的
动作创建者
定义为:

export function getCoords(position) {
    console.log(position)
    return {
        type: 'GET_LOCATION',
        long: position.coords.longitude,
        lat: position.coords.latitude
    }
}

getLocation
函数修复为:

函数getLocation(){ 返回新承诺((解决、拒绝)=>{ if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(位置=>{ 展示位置(pos); 解决(pos); }); }否则{ 拒绝(“此浏览器不支持地理位置。”) } )}; }