I am using a foreach loop to execute a single query with different variable multiple time by the length of data.I am using async to do this so that I can do this with synchronization. But when I am using callback function I get type-error. After inserting the first row in database its stopped inserting for the error. I have search several solution.But nothing solved my problem. Don't know what to do next.If you can find any solution to this code Thanks in advance.
var async = require('async');
async.forEachOfSeries(data, function (dataElement, callback){
request.input('input_id', mssql.Int,dataElement.id);
request.input('input_service_name', mssql.VarChar(25),dataElement.service_name );
request.input('input_msisdn',mssql.VarChar(255),dataElement.msisdn);
request.input('input_sms',mssql.Text,dataElement.sms);
//request.input('input_datetime',mssql.DateTime,data[i].datetime);
request.input('input_smsid',mssql.BigInt,dataElement.smsid);
request.input('input_status',mssql.VarChar(20),dataElement.status);
request.input('input_txid',mssql.VarChar(200),dataElement.txid);
request.query("insert into mt_log (id,service_name,msisdn,sms,smsid,status,txid) values (@input_id,@input_service_name,@input_msisdn,@input_sms,@input_smsid,@input_status,@input_txid)", (err, result) => {
if(err){
console.log(err);
callback();
}
else{
console.log("inserting");
callback();
}
});
}, function(err){
if(err){
//handle the error if the query throws an error
}else{
//whatever you wanna do after all the iterations are done
}
});
Error
D:\LCT Work\node project\Datashiftingtolct2\server.js:109
callback();
^
TypeError: callback is not a function
at request.query (D:\LCT Work\node project\Datashiftingtolct2\server.js:109:21)
at _query (D:\LCT Work\node project\Datashiftingtolct2\node_modules\mssql\lib\base.js:1347:9)
at Request.tds.Request.err [as userCallback] (D:\LCT Work\node project\Datashiftingtolct2\node_modules\mssql\lib\tedious.js:671:15)
at Request.callback (D:\LCT Work\node project\Datashiftingtolct2\node_modules\tedious\lib\request.js:37:27)
at Connection.endOfMessageMarkerReceived (D:\LCT Work\node project\Datashiftingtolct2\node_modules\tedious\lib\connection.js:2104:20)
at Connection.dispatchEvent (D:\LCT Work\node project\Datashiftingtolct2\node_modules\tedious\lib\connection.js:1084:36)
at Parser.tokenStreamParser.on (D:\LCT Work\node project\Datashiftingtolct2\node_modules\tedious\lib\connection.js:914:14)
at Parser.emit (events.js:198:13)
at Parser.parser.on.token (D:\LCT Work\node project\Datashiftingtolct2\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
at Parser.emit (events.js:198:13)
Here the 109 line is the callback(); in else statement