节点async.js:回调不是函数错误?
Node async.js: callback is not a function error?
我有这个代码:
async = require('async')
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(callback, results) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(
function() {
callback(null, userExist);
},
1000
);
}
}
],
signup: [
'checkIfUserExist',
function(callback, results) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = result.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
]
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
为什么我遇到这个错误:
**In getUserName.**
**In connectToDb.**
error = function () {
if (fn === null) throw new Error("Callback was already called.");
var callFn = fn;
fn = null;
callFn.apply(this, arguments);
}
results = undefined
**In checkIfUserExist.** undefined
^[[A/home/lem/js/async/asyncAuto.js:30
callback(null, userExist);
^
TypeError: callback is not a function
at Timeout._onTimeout (/home/lem/js/async/asyncAuto.js:30:13)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
一个异步函数,一个期望回调作为其最后一个参数的函数。这是我的工作代码。
var async = require('async');
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(results, callback) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(function() {
callback(null, userExist);
},1000);
}
}
],
signup: [
'checkIfUserExist',
function(results, callback) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = results.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
],
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
function(callback, results) 应该是 function(results, callback)
请检查此 url 以供参考。
https://caolan.github.io/async/docs.html#auto
v2 中异步的 auto
方法发生了重大变化,您似乎正在使用它。
https://github.com/caolan/async/blob/master/CHANGELOG.md#breaking-changes
auto task functions now always take the callback as the last argument. If a task has dependencies, the results object will be passed as the first argument. To migrate old task functions, wrap them with _.flip
我有这个代码:
async = require('async')
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(callback, results) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(
function() {
callback(null, userExist);
},
1000
);
}
}
],
signup: [
'checkIfUserExist',
function(callback, results) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = result.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
]
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
为什么我遇到这个错误:
**In getUserName.**
**In connectToDb.**
error = function () {
if (fn === null) throw new Error("Callback was already called.");
var callFn = fn;
fn = null;
callFn.apply(this, arguments);
}
results = undefined
**In checkIfUserExist.** undefined
^[[A/home/lem/js/async/asyncAuto.js:30
callback(null, userExist);
^
TypeError: callback is not a function
at Timeout._onTimeout (/home/lem/js/async/asyncAuto.js:30:13)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
一个异步函数,一个期望回调作为其最后一个参数的函数。这是我的工作代码。
var async = require('async');
async.auto({
getUserName: function(callback) {
console.log('**In getUserName.**')
callback(null, 'Lem')
},
connectToDb: function(callback) {
console.log('**In connectToDb.**')
var connected = true
if(connected) {
callback(null, connected)
} else {
callback('Error connecting to DB.', null)
}
},
checkIfUserExist: [
'getUserName',
'connectToDb',
function(results, callback) {
console.log('**In checkIfUserExist.**',
JSON.stringify(results))
var userExist = false
if(userExist) {
callback('User exist in DB.')
} else {
setTimeout(function() {
callback(null, userExist);
},1000);
}
}
],
signup: [
'checkIfUserExist',
function(results, callback) {
console.log('**In signup**', JSON.stringify(results))
var userName = results.getUserName
var isDbConnected = results.connectToDb
var userExist = results.checkIfUserExist
if(userName && isDbConnected && !userExist) {
callback(null,
{'status': '200', 'msg': 'Successfully signed up user'})
} else {
callback('Error signing up user.', null)
}
}
],
},
function(error, results) {
console.log('error = ', error)
console.log('results = ', results)
})
function(callback, results) 应该是 function(results, callback)
请检查此 url 以供参考。 https://caolan.github.io/async/docs.html#auto
v2 中异步的 auto
方法发生了重大变化,您似乎正在使用它。
https://github.com/caolan/async/blob/master/CHANGELOG.md#breaking-changes
auto task functions now always take the callback as the last argument. If a task has dependencies, the results object will be passed as the first argument. To migrate old task functions, wrap them with _.flip