0

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

3
  • It would be easy to tell if you attach the error also. Commented Aug 22, 2019 at 5:16
  • I am attaching it right now. @Huzaifa Commented Aug 22, 2019 at 5:16
  • i have updated the code with error. Commented Aug 22, 2019 at 5:21

1 Answer 1

1

Assuming you're using the async library v1.5x, per the documentation, the function you pass to async.forEachOfSeries() has three arguments, not two and the callback arrives in the third argument, not the second.

So, you're trying to call something that isn't a function (thus the error you see), it's actually the key argument, not the callback argument.

Change this:

async.forEachOfSeries(data, function (dataElement, callback){

to this:

async.forEachOfSeries(data, function (dataElement, key, callback){
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much for your effort .I am trying this.
Thank you very much @jfriend00. It worked.Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.