2017-12-17 5 views
1

JavaScriptでプロミスに問題がありました。私がしようとしているのは、アドレスのリストを取得した後、各アドレスについて、地理座標APIを呼び出してlat lngを取得する必要があります。その後、ヒートマップとともにマーカーをプロットするように進みます。 )問題は今、プログラムはちょうどgetBranchLatLng(で停止JavaScriptプロミスを実行していません.then()

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; 
    } 

}); 
} 

)と偶数(addForecastMarkerに入ることはありません:

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

     var addedMarkers = promiseKey.then(
     markers => Promise.all(
      markers.map() 
     ) 
     ) 
     .then(drawForecastHeatmap); 

私はジオコードAPIを呼び出す部分:ここに私のコードです私はジオコードからいくつかの緯度をプリントアウトすることができました。

私を返すアドレスの一部:

error_message: "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter." 
results :[] 
status: "INVALID_REQUEST" 

「は、それらのアドレスを解決する方法上の任意のアイデアをINVALID_REQUEST:私は、リンクを拡張しようとすると、

jquery.min.js:4 GET https://maps.googleapis.com/maps/api/geocode/json?key=&address= 400() 

はその後、私はこれを取得していますプロミスの親に戻る?

ありがとうございます!

+3

あなたはgetBranchLatLng mybeで拒否を使用しないでください。エラーが表示され、それが約束が止まる理由です。 –

+0

@AmitWagnerあなたはそのチャンクをcatch()する必要がありますか? – hyperfkcb

+0

あなたのAjax関数では、何らかのエラーがある可能性があります。あなたがコードを継続するかどうかを確認するために解決しようとするか、または拒否を使用して、約束のチェーンのエラーを処理してください –

答えて

0

コードで使用しているすべてのテストデータと宣言されていない変数を持たないで、ワーキングコードを生成することはできません。しかし私が提案できることは、catchブロックをajaxリクエストに追加し、resolvingまたはrejectingという約束がないワークフローがgetBranchLatLng Promiseに存在しないことに注意してください。

何のワークフローがしますないresolvereject

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'); 
} 
あなた getBranchLatLng約束ではありませんように、何らかのエラーの包み、あなたはまだ getBranchLatLngを解決したい、と仮定すると、私は、このようなあなたのコードを更新しました

これは、最終結果を直接取得するのに使用できる定型句コードではありません。いずれかの約束が失敗した場合は、エラー処理コードを追加する必要があります。あなたの助けのために、私はこれらの約束をtype: 'error'で解決しました。その後ブロックpromiseKeyの中でそれらを読んで、さらにエラー処理を行い、アレイ上のエラーを処理してからaddForecastMarkerを呼び出す必要があります。

希望するとうれしいです。

+0

私は見るのを見ました!本当にありがとう!私はそれを働かせることができた。何度かテストしましょう! – hyperfkcb

+0

素晴らしい!喜んで:) –

関連する問題