the data obtained by the foreground is in the form of an array. How to insert the data into the database after a loop? at present, this code cannot be looped. Why can only one piece of data be inserted in the database? Ask for guidance, thank you!
module.exports = function (sender) {
var yjDBService = global.yjRequire("yujiang.Foil").yjDBService;
var _getNewSN = global.yjRequire("yujiang.Foil.Biz","sn/_getNewSN.js");
var async = require("async");
var data = sender.req.body;
/ / above are referenced corporate frameworks and some components
var events = {
"inserted" : insertData,
"updated" : updateData
}
var fn = null;
for(var i in data){
fn = events[i];
var param = data[i];
if(fn){
fn(param);
}else{
sender.error(new Error("Param is not found!"))
}
}
function insertData(data){
var events = [];
for (var i = 0; i < data.length; iPP) {
(function(i){
var obj = data[i];
function insert(callback){
_getNewSN({
query : {category : "ProjectOID_" + obj.ProjectOID},
success : function(sn){
var param = [parseInt(obj.FunctionCode),
parseInt(obj.StartRegister) ,
parseInt(obj.Length) ,
obj.Description,
obj.ProjectHardwareOID ,
obj.Port,sn.SN];
var sql = "insert into " +
"plcModbusDB(FunctionCode,StartRegister," +
"Length,Description,ProjectHardwareOID," +
"Port,DataID) values(?,?,?,?,?,?,?)";
yjDBService.exec({
sql:sql,
parameters : param,
success : sender.success,
error : sender.error
});
},
error : sender.error
})
}
events.push(insert);
})(i)
}
async.waterfall(events,function(err,result){
if(err){
sender.error(err);
}else{
sender.success(result);
}
})
}
function updateData(data){
var sql = "update plcModbusDB " +
"set FunctionCode=?,StartRegister=?,Length=?,Description=? " +
"where OID = ?";
yjDBService.exec({
sql:sql,
parameters : [data.FunctionCode , data.StartRegister , data.Length , data.Description , data.OID],
success : sender.success,
error : sender.error
});
}
}