Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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承诺不执行。然后()_Javascript_Asynchronous_Promise_Geocode - Fatal编程技术网

JavaScript承诺不执行。然后()

JavaScript承诺不执行。然后(),javascript,asynchronous,promise,geocode,Javascript,Asynchronous,Promise,Geocode,我对JavaScript中的Promise有一些问题。我想做的是得到一个地址列表,然后对于每个地址,我需要调用GeocodeAPI来获得lat lng,然后我将继续绘制标记和热图。这是我的密码: let promiseKey = Promise.all( result.map() ); var addedMarkers = promiseKey.then( markers => Promise.all(

我对JavaScript中的Promise有一些问题。我想做的是得到一个地址列表,然后对于每个地址,我需要调用GeocodeAPI来获得lat lng,然后我将继续绘制标记和热图。这是我的密码:

let promiseKey = Promise.all(
          result.map()
        );

        var addedMarkers = promiseKey.then(
        markers => Promise.all(
          markers.map()
        )
        )
        .then(drawForecastHeatmap);
我调用地理代码API的部分:

function getBranchLatLng(address, branchName, total, queKey){
return new Promise(function(resolve){
    var key = jQuery.rand(geoCodKeys);
    var url = 'https://maps.googleapis.com/maps/api/geocode/json?key='+key+'&address='+address+'&sensor=false';

    $.ajaxq (qyName, {
        url: url,
        dataType: 'json'
    }).done(function( data ) {
        var address = getParameterByName('address', this.url);
        var index = errorArray.indexOf(address);
        try{
            var p = data.results[0].geometry.location;
            var latlng = new google.maps.LatLng(p.lat, p.lng);

            var markerItem =
                {
                    'lat': p.lat,
                    'lng': p.lng,
                    'address': address,
                    'branchName': branchName,
                    'total': total,
                };
            console.log(markerItem);
            resolve(markerItem);

            if (index > -1) {
                errorArray.splice(index, 1);
            }
        }catch(e){
            if(data.status = 'ZERO_RESULTS')
                return false;

            //on error call add marker function for same address and keep in Error ajax queue
            getBranchLatLng( address, 'Error' );
            if (index == -1) {
                errorArray.push( address );
            }
        }
    });

    //mentain ajax queue set
    queuCounter++;
    if( queuCounter == setLimit ){
        queuCounter = 0;
    }

});
}
现在的问题是,程序只是在getBranchLatLng()处停止,甚至从未进入addForecastMarker(),尽管我设法从地理代码中打印出了一些lat lng

返回给我的一些地址:

jquery.min.js:4 GET https://maps.googleapis.com/maps/api/geocode/json?key=&address= 400 ()
然后,当我尝试扩展链接时,我得到以下结果:

error_message: "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter."
results :[]
status: "INVALID_REQUEST"
关于如何将“无效请求”回复给承诺家长解决这些地址,有什么想法吗


谢谢

如果代码中没有使用的所有测试数据和未声明的变量,则无法生成工作代码。但总的来说,我可以建议,您必须在ajax请求中添加一个
catch
块,并且要记住,在
getBranchLatLng Promise
中不能有任何工作流不是
解决
拒绝
承诺

假设在出现任何错误时,您仍然希望解决
getBranchLatLng
,我已经像这样更新了您的代码,这样您的
getBranchLatLng
承诺中就没有工作流不会
解决
拒绝

let promiseKey = Promise.all(
          result.map(el=>getBranchLatLng(el.branchAddress, el.branchName, el.amount))
        );

        var addedMarkers = promiseKey.then(
        markers => Promise.all(
          markers.map(marker => addForecastMarker(marker))
        )
        )
        .then(drawForecastHeatmap)
        .catch(functon(e) {
            //Do the error handling here
        });

function getBranchLatLng(address, branchName, total, queKey) {
return new Promise(function(resolve, reject) {
    var key = jQuery.rand(geoCodKeys);
    var url = 'https://maps.googleapis.com/maps/api/geocode/json?key='+key+'&address='+address+'&sensor=false';

    var qyName = '';
    if( queKey ) {
        qyName = queKey;
    } else {
        qyName = 'MyQueue'+queuCounter;
    }

    $.ajaxq (qyName, {
        url: url,
        dataType: 'json'
    }).done(function( data ) {
        var address = getParameterByName('address', this.url);
        var index = errorArray.indexOf(address);
        try{
            var p = data.results[0].geometry.location;
            var latlng = new google.maps.LatLng(p.lat, p.lng);

            var markerItem =
                {
                    'lat': p.lat,
                    'lng': p.lng,
                    'address': address,
                    'branchName': branchName,
                    'total': total,
                };
            console.log(markerItem);
            if (index > -1) {
                errorArray.splice(index, 1);
            }
            resolve(markerItem);

        }catch(e) {
                if (index == -1) {
                errorArray.push( address );
            }
                resolve({type: 'error', status: data.status});

        }
    })
    .catch(function(e) {
        resolve({type: 'error', status: data.status});
      //Instead of resolving with error code you can also reject the promise
      //reject(e);
    });

    //mentain ajax queue set
    queuCounter++;
    if( queuCounter == setLimit ){
        queuCounter = 0;
    }

  }
)};

function addForecastMarker(marker) {
console.log('adddddd markerssssss');
}
但这不是可以直接获得最终结果的样板代码。当任何承诺都将失败时,您需要添加错误处理代码。为了得到您的帮助,我用
键入:“error”
解决了这些承诺。在
promiseKey
的then块中,在调用
addForecastMarker
之前,必须先读取这些内容,然后执行进一步的错误处理,并从数组中删除这些内容


希望它能有所帮助。

您在getBranchLatLng mybe中不会使用reject,因为您会遇到错误,这就是承诺的原因stops@AmitWagner你的意思是去掉那个块的catch()?我的意思是在ajax函数中可能有一些错误。尝试所有方式解析,查看您是继续编写代码还是使用拒绝并处理承诺链上的错误查看catch i add resolve to only for test中的代码。您需要在catch和handle中使用reject。首先,您缺少各种错误处理。因此,如果
Promise.all()
调用中的任何承诺失败,那么其他代码都不会运行。到处都需要错误处理。甚至可以告诉你这里发生了什么。这是一个教科书上的坏承诺代码的例子,它忽略了所有的错误。例如,如果ajax调用失败怎么办?您什么都不做-父承诺不会被拒绝或解决,因此
promise.all()
调用将永远等待。我明白了,我明白了!非常感谢!我设法使它运转起来。让我再测试几次!伟大的很乐意帮忙:)