我在项目中使用async的流程控制功能,想把task1查询到结果,传到task2中使用,但是却报了TypeError: callback is not a function,除此之外没有任何提示,我不知道是哪里处理问题,我是第一次接触nodejs,请各位大神帮忙看下是什么原因造成的这个错误,在此拜托了。 db.getTransaction(function(mssql,transaction){ //开启事务 transaction.begin(function(err){ if(err){ console.log(err); return; } //定义一个变量,如果自动回滚,则监听回滚时间并修改为true,无需手动回滚 var rolledBack = false; //监听回滚事件 transaction.on(‘rollback’,function(aborted){ console.log(‘listen rollback’); console.log(‘aborted值’+aborted); rolledBack = true; }); //监听提交事件 transaction.on(‘commit’,function(){ console.log(‘listen commit’); rolledBack = true; }); var request = new mssql.Request(transaction);
async.auto({
//查询T_ORD_ORDER的最新的ORD_ID字段
task1:function(callback,result){
request.query(sql3,function(err,result){
if(err){
console.log("task1 error:"+err);
callback(err,null);
return;
}
var ord_id = result[0].oid;
console.log("company_id="+req.session.user[0].company_id);
console.log("ord_id="+result[0].onum);
ords_id = result[0].onum;
console.log("ords_idaaaaaaaaaaaa:"+ords_id);
console.log("task1result"+result);
callback(null,result);
})
},
//插入记录到T_ORD_CARGO表
task2:['task1',function(callback,result){
console.log("ords_id======================================"+ords_id);
var sql2;
for(var i=1; i<crgbarcodearr.length;i++){
//sql2 = "insert into idealwl.dbo.T_ORD_CARGO(";
sql2 = "insert into zhaobin.dbo.T_ORD_CARGO2(";
if(crgbarcodearr[i]!=''){
sql2 += "CRG_BARCODE,";
}
sql2 += "CRG_NAME,";
if(crgkingdescarr[i]!=''){
sql2 += "CRG_KIND_DESC,";
}
if(crgvolumearr[i]!=''){
sql2 += "CRG_VOLUME,";
}
if(crgweightarr[i]!=''){
sql2 += "CRG_WEIGHT,";
}
if(crgfeeganxianarr[i]!=''){
sql2 += "CRG_FEE_GANXIAN,";
}
if(crgcountarr[i]!=''){
sql2 += "CRG_COUNT,";
}
if(carmemosarr[i]!=''){
sql2 += "MEMOS,";
}
sql2 += "ORD_ID";
sql2+=") values('";
if(crgbarcodearr[i]!=''){
sql2+=crgbarcodearr[i]+"','";
}
sql2+=crgnamearr[i]+"','";
if(crgkingdescarr[i]!=''){
sql2+=crgkingdescarr[i]+"',";
}
if(crgvolumearr[i]!=''){
sql2+=crgvolumearr[i]+",";
}
if(crgweightarr[i]!=''){
sql2+=crgweightarr[i]+",";
}
if(crgfeeganxianarr[i]!=''){
sql2+=crgfeeganxianarr[i]+",";
}
if(crgcountarr[i]!=''){
sql2+=crgcountarr[i]+",'";
}
if(carmemosarr[i]!=''){
sql2+=carmemosarr[i]+"',";
}
sql2 += ords_id+")";
console.log("sql2:"+sql2);
}
request.query(sql2,function(err,result){
if(err){
console.log("task2 error:"+err);
callback(err,null);
return;
}
callback(null,result);----------------------------> 在这个地方报错
})
}],
D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:543
callback(null,result);
^
TypeError: callback is not a function at D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:543:7 at D:\workspace\YiXinCloudGuanjiaDemo\node_modules\mssql\lib\main.js:1588:20 at Request.userCallback (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\mssql\lib\tedious.js:853:61) at Request.callback (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\tedious\lib\request.js:33:27) at Connection.message (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\tedious\lib\connection.js:1179:27) at Connection.dispatchEvent (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\tedious\lib\connection.js:519:45) at MessageIO.<anonymous> (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\tedious\lib\connection.js:439:23) at emitNone (events.js:86:13) at MessageIO.emit (events.js:185:7) at ReadablePacketStream.<anonymous> (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\tedious\lib\message-io.js:92:15)
你的async.auto方法用错了,看下官方的例子吧:
async.auto({
get_data: function(callback) {
console.log('in get_data');
callback(null, 'data', 'converted to array');
},
make_folder: function(callback) {
console.log('in make_folder');
callback(null, 'folder');
},
//这里的回调第一个参数是result,第二参数才是callback!
write_file: ['get_data', 'make_folder', function(results, callback) {
console.log('in write_file', JSON.stringify(results));
callback(null, 'filename');
}],
email_link: ['write_file', function(results, callback) {
console.log('in email_link', JSON.stringify(results));
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
明显跟着数组时,result和callback入参顺序反过来了,你写的callback其实是result,当然不是Function了。 遇到这种问题建议直接去看官方最新的API文档,百度到的东西可能会有时效性错误