登录
注册
node.js 学习社区
使用nodejs async时,报TypeError: callback is not a function错误

zh19860820

2017-03-10 14:59

我在一个项目中使用async.auto时,报了TypeError: callback is not a function错误

具体代码如下


var ords_id ="";

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表

task1:function(callback,result){

request.query(sql,function(err,result){

if(err){

console.log("task1 error:"+err);

callback(err,null);

return;

}

callback(null,result);


})

},

//更新T_SHR_SEQNO的SEQNO字段

task2:function(callback,result){

request.query(sql1,function(err,result){

if(err){

console.log("task2 error:"+err);

callback(err,null);

return;

}

callback(null,result);

console.log("task2result:"+result);

})

},

//查询T_ORD_ORDER的最新的ORD_ID字段

task3:function(callback,result){

request.query(sql3,function(err,result){

if(err){

console.log("task3 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("task3result"+result);

callback(null,result);

})

},

//插入记录到T_ORD_CARGO表

task4:['task3',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(";

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]+"','";

//console.log("crgbarcode is existed");

}

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);

console.log("task3:"+util.inspect(result));

}


request.query(sql2,function(err,result){

if(err){

console.log("task4 error:"+err);

callback(err,null);

return;

}

console.log("task4result"+result);

callback(null,result);-------> 报错的地方

})

}]

},function(err,result){

if(err){

console.log('sql error,rollback');

if(!rolledBack){

//如果sql语句错误会自动回滚,如果程序错误手动执行回滚,不然事物会一直挂起.

transaction.rollback(function(err){

if(err){

console.log("rollback error"+err);

return;

}

console.log('rollback success');

});

}

}else{

console.log('no error,commit');

//执行提交

transaction.commit(function(err){

if(err){

console.log('commit error:'+err);

return;

}

console.log('commit success');

//console.log("result:"+result);

res.redirect('/user/query/1');

});

}

});

});

});

});

错误如下


D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:567

callback(null,result);

^


TypeError: callback is not a function

at D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:567: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)

请各位大神帮忙解答,谢谢




回复 · 0

发表回复

你可以在回复中 @ 其他人