gpt4 book ai didi

node.js - Node async.js : callback is not a function error?

转载 作者:搜寻专家 更新时间:2023-11-01 00:21:38 33 4
gpt4 key购买 nike

我有这个代码:

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)

最佳答案

v2 中 async 的 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

关于node.js - Node async.js : callback is not a function error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40189273/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com