2016-04-25 257 views
0

我正在全天搜索論壇,但無法解決我的問題。我使用NodeJS和async.waterfall來提出一些API請求。 同樣的結構適用於我,但不適用於此;Node.js「回調已被調用」。但沒有任何其他回調

async.waterfall([ 
     function (callback) { 
      callback(null, latitude, longitude, callback); 
     }, 
     getGeoNames, 
    ], 
    function (err, result) { 
     console.log("ok"); 
    }) 

,這裏是功能getGeoNames錯誤

function getGeoNames(latitude, longitude, callback) { 
    var countryKey = 'country'; 
    var stateKey = 'administrative_area_level_1'; 

    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+latitude+","+longitude; 


    request({ 
     url: url, 
     json: true 
    }, function (error, response, body) { 

      var adresses = body.results[0].address_components; 
      var geo = {}; 
      for (var i = 0, len = adresses.length; i < len; i++) { 
       var adress = adresses[i]; 
       //console.log(adress); 
       if (adress.types[0] == countryKey) { 
        geo.country = adress.long_name; 
        if (geo.state !== undefined) break; 
       } 
       else if (adress.types[0] == stateKey) { 
        geo.state = adress.long_name; 
        if (geo.country !== undefined) break; 
       } 
      }; 
      return callback(null, geo); // ERROR HERE <----------------- 
    }) 
} 

可以要求功能運行的回調()函數,所以有人拼命地跑前? 我試過沒有「返回」,但它是一樣的。

 if (fn === null) throw new Error("Callback was already called."); 
         ^

Error: Callback was already called. at c:\Users\Serega\node_modules\async\dist\async.js:803:36 at Request._callback (c:\Users\Serega\WebstormProjects\untitled1\server.js:174:20) at Request.self.callback (c:\Users\Serega\node_modules\request\request.js:200:22) at emitTwo (events.js:87:13) at Request.emit (events.js:172:7) at Request. (c:\Users\Serega\node_modules\request\request.js:1067:10) at emitOne (events.js:82:20) at Request.emit (events.js:169:7) at IncomingMessage. (c:\Users\Serega\node_modules\request\request.js:988:12) at emitNone (events.js:72:20)

Process finished with exit code 1

PS:其他瀑布函數與我的請求一起工作。

感謝您的幫助!

回答

0

錯誤在這裏。

callback(null, latitude, longitude, callback); 

您不應該將回調作爲參數傳遞。

+0

Спасибоогромное - помогло:) –