Node.js "Callback is already called"。但是没有任何其他回调
Node.js "Callback is already called". But there are no any other callbacks
整天都在找论坛,还是解决不了我的问题。我正在使用 NodeJS 和 async.waterfall 发出一些 API 请求。
相同的结构适用于我,但不适用;
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 <-----------------
})
}
可以请求函数 运行 callback() 函数,所以它之前是 运行ned 的?
我试过没有“return”,但它是一样的。
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: 其他瀑布函数可以根据我的要求使用。
感谢您的帮助!
错误在这里。
callback(null, latitude, longitude, callback);
您不应将回调作为参数传递。
整天都在找论坛,还是解决不了我的问题。我正在使用 NodeJS 和 async.waterfall 发出一些 API 请求。 相同的结构适用于我,但不适用;
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 <-----------------
})
}
可以请求函数 运行 callback() 函数,所以它之前是 运行ned 的? 我试过没有“return”,但它是一样的。
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: 其他瀑布函数可以根据我的要求使用。
感谢您的帮助!
错误在这里。
callback(null, latitude, longitude, callback);
您不应将回调作为参数传递。