怎样用nodejs async实现在插入一张表的数据时返回该表的主键插入到另一张表的外键字段
我想实现在插入一张表的数据时返回该表的主键,然后将该主键插入到另一张表的外键字段
我想通过async.auto来实现这个功能,但是出现了TypeError: callback is not a function
我现在贴上代码,请大家帮忙看看该怎么解决
.post(function(req,res){
//T_ORD_ORDER表字段
var orderbarcode = req.body.ORDER_BARCODE;
var ordcustname = req.body.ORD_CUST_NAME;
var ordcustcontact = req.body.ORD_CUST_CONTACT;
var orderextbarcode = req.body.ORDER_EXTBARCODE;
var createby = req.body.CREATEBY;
var updateby = req.body.UPDATEBY;
var ordergoodscount = req.body.ORDER_GOODS_COUNT;
var ordervolume = req.body.ORDER_VOLUME;
var orderweigth = req.body.ORDER_WEIGTH;
var orderinsuamount = req.body.ORDER_INSU_AMOUNT;
var orderinsufee = req.body.ORDER_INSU_FEE;
var ordcustremarks = req.body.ORD_CUST_REMARKS;
//T_SHR_SEQNO表SEQNO字段
var seqno = req.body.seqno;
//T_ORD_CARGO表字段
var crgbarcode = req.body.CRG_BARCODE;//产品型号
console.log(crgbarcode);
var crgbarcodearr = crgbarcode.toString().split(',');
console.log("crgbarcodearr.length:"+crgbarcodearr.length);
//console.log("crgbarcodearr:"+crgbarcodearr[0]);
var crgname = req.body.CRG_NAME;//产品名称
var crgnamearr = crgname.toString().split(',');
console.log("crgnamearr.length:"+crgnamearr.length);
console.log("crgnamearr:"+crgnamearr[0]);
var crgkingdesc = req.body.CRG_KIND_DESC;//产品类型
var crgkingdescarr = crgkingdesc.toString().split(',');
console.log("crgkingdescarr:"+crgkingdescarr[0]);
var crgvolume = req.body.CRG_VOLUME;//体积 (M3)
var crgvolumearr = crgvolume.toString().split(',');
console.log("crgvolumearr:"+crgvolumearr[0]);
var crgweight = req.body.CRG_WEIGHT;//重量 (T)
var crgweightarr = crgweight.toString().split(',');
console.log("crgweightarr:"+crgweightarr[0]);
var crgfeeganxian = req.body.CRG_FEE_GANXIAN;//干线费
var crgfeeganxianarr = crgfeeganxian.toString().split(',');
console.log("crgfeeganxianarr:"+crgfeeganxianarr[0]);
var crgcount = req.body.CRG_COUNT;//件数
var crgcountarr = crgcount.toString().split(',');
console.log("crgcountarr:"+crgcountarr[0]);
var carmemos = req.body.carMemos;//备注
var carmemosarr = carmemos.toString().split(',');
console.log("carmemosarr:"+carmemosarr[0]);
//var orderweigth = req.body.ORDER_WEIGTH;
var sql =
"insert into idealwl.dbo.T_ORD_ORDER(";
//"insert into zhaobin.dbo.T_ORD_ORDER(";
if(orderbarcode){
sql+="ORDER_BARCODE,";
}
sql+="ORD_CUST_NAME,";
if(ordcustcontact){
sql+="ORD_CUST_CONTACT,";
}
if(orderextbarcode){
sql+="ORDER_EXTBARCODE,";
}
if(createby){
sql+="CREATEBY,";
}
if(updateby){
sql+="UPDATEBY,";
}
if(ordergoodscount){
sql+="ORDER_GOODS_COUNT,";
}
if(ordervolume){
sql+="ORDER_VOLUME,";
}
if(orderweigth){
sql+="ORDER_WEIGTH,";
}
if(orderinsuamount){
sql+="ORDER_INSU_AMOUNT,";
}
if(orderinsufee){
sql+="ORDER_INSU_FEE,";
}
if(ordcustremarks){
sql+="ORD_CUST_REMARKS,"
}
if(req.session.user[0].company_id){
sql+="COMPANY_ID";
}
sql+=") values('";
if(orderbarcode){
sql+=orderbarcode;
}
sql+="','"+ordcustname;
if(ordcustcontact){
sql+="','"+ordcustcontact;
}
if(orderextbarcode){
sql+="','"+orderextbarcode;
}
if(createby){
sql+="','"+createby;
}
if(updateby){
sql+="','"+updateby;
}
if(ordergoodscount){
sql+="',"+ordergoodscount;
}
if(ordervolume){
sql+=","+ordervolume;
}
if(orderweigth){
sql+=","+orderweigth;
}
if(orderinsuamount){
sql+=","+orderinsuamount;
}
if(orderinsufee){
sql+=","+orderinsufee;
}
if(ordcustremarks){
sql+=",'"+ordcustremarks;
}
if(req.session.user[0].company_id){
sql+="',"+req.session.user[0].company_id+")";
}
//var sql = "insert into zhaobin.dbo.T_ORD_ORDER(ORDER_BARCODE,ORDER_SENDER_NAME,ORDER_SENDER_ADDR,ORDER_SENDER_ORG_NAME,ORDER_RECV_NAME,ORDER_RECV_ADDR,ORDER_RECV_ORG_NAME,CREATETIME,UPDATETIME) values('"+orderbarcode+"','"+ordersendername+"','"+ordersenderaddr+"','"+ordersenderorgname+"','"+orderrecvname+"','"+orderrecvaddr+"','"+orderrecvorgname+"','"+createtime+"','"+updatetime+"')";
var sql1 =
"update idealwl.dbo.T_SHR_SEQNO set SEQNO = "+seqno;
//"update zhaobin.dbo.T_SHR_SEQNO set SEQNO = "+seqno;
var sql3 = "select TOP 1 ORD_ID from idealwl.dbo.T_ORD_ORDER order by ORD_ID desc";
console.log("sql:"+sql);
console.log("sql1:"+sql1);
console.log("sql3:"+sql3);
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({
task1:function(callback){
request.query(sql,function(err,result){
if(err){
console.log(err);
callback(err,null);
return;
}
callback(null,result);
})
},
task2:function(callback){
request.query(sql1,function(err,result){
if(err){
console.log(err);
callback(err,null);
return;
}
callback(null,result);
console.log("task2result:"+result);
})
},
task3:function(callback){
request.query(sql3,function(err,result){
if(err){
console.log(err);
callback(err,null);
return;
}
var ord_id = result[0].ORD_ID;
callback(null,{"ord_id":ord_id});
// for ( var r in result) {
// console.log("result:"+result[r].toString());
// }
// console.log("result[0]:"+result[0]);
// console.log("result[0].ORD_ID:"+result[0].ORD_ID);
//console.log("task3result:"+result);
})
//console.log("result:"+result);
},
task4:["task3",function(callback,result){
var sql2;
for(var i=1; i<crgbarcodearr.length;i++){
sql2 = "insert into idealwl.dbo.T_ORD_CARGO(";
//var sql2 = "insert into zhaobin.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]+"',";
//console.log("crgname is existed");
if(crgkingdescarr[i]!=''){
sql2+=crgkingdescarr[i]+"',";
//console.log("crgkingdesc is existed");
}
if(crgvolumearr[i]!=''){
sql2+=crgvolumearr[i]+",";
//console.log("crgvolume is existed");
}
if(crgweightarr[i]!=''){
sql2+=crgweightarr[i]+",";
//console.log("crgweight is existed");
}
if(crgfeeganxianarr[i]!=''){
sql2+=crgfeeganxianarr[i]+",";
//console.log("crgfeeganxian is existed");
}
if(crgcountarr[i]!=''){
sql2+=crgcountarr[i]+",'";
//console.log("crgcount is existed");
}
if(carmemosarr[i]!=''){
sql2+=carmemosarr[i]+"' )";
//console.log("carmemos is existed");
}
console.log("sql2:"+sql2);
}
request.query(sql2,function(err,result){
if(err){
console.log(err);
callback(err,null);
return;
}
callback(null,result);
//console.log("task4result:"+result);
})
callback(null,result);
//console.log("ord_id:"+result[0].ORD_ID);
//console.log("result:"+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:workspaceYiXinCloudGuanjiaDemoroutesuser.js:592
callback(null,result);
^
TypeError: callback is not a function
at async.auto.task4 (D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:592:6)
at runTask (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:1626:13)
at D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:1568:13
at processQueue (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:1578:13)
at taskComplete (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:1596:9)
at D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:1619:17
at apply (D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:21:25)
at D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:56:12
at D:\workspace\YiXinCloudGuanjiaDemo\node_modules\async\dist\async.js:840:16
at D:\workspace\YiXinCloudGuanjiaDemo\routes\user.js:508:7
出错的代码如下
task4:["task3",function(callback,result){
var sql2;
for(var i=1; i<crgbarcodearr.length;i++){
sql2 = "insert into idealwl.dbo.T_ORD_CARGO(";
//var sql2 = "insert into zhaobin.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]+"',";
//console.log("crgname is existed");
if(crgkingdescarr[i]!=''){
sql2+=crgkingdescarr[i]+"',";
//console.log("crgkingdesc is existed");
}
if(crgvolumearr[i]!=''){
sql2+=crgvolumearr[i]+",";
//console.log("crgvolume is existed");
}
if(crgweightarr[i]!=''){
sql2+=crgweightarr[i]+",";
//console.log("crgweight is existed");
}
if(crgfeeganxianarr[i]!=''){
sql2+=crgfeeganxianarr[i]+",";
//console.log("crgfeeganxian is existed");
}
if(crgcountarr[i]!=''){
sql2+=crgcountarr[i]+",'";
//console.log("crgcount is existed");
}
if(carmemosarr[i]!=''){
sql2+=carmemosarr[i]+"' )";
//console.log("carmemos is existed");
}
console.log("sql2:"+sql2);
}
request.query(sql2,function(err,result){
if(err){
console.log(err);
callback(err,null);
return;
}
callback(null,result);
//console.log("task4result:"+result);
})
callback(null,result);
//console.log("ord_id:"+result[0].ORD_ID);
//console.log("result:"+result);
}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论