1

I am reasonably new to node.js / sails.js and have run into a problem that I know the answer is simple but I cannot seem to work it out.

My code is as follows

SendCompleted : function(req,res)
    {
        var updated = 0;
        var params = req.params.all();
        var dt = JSON.parse(decodeURI(params.id));
        var connection = new sql.Connection(testrmis, function (err)
        {
            if (err) {

            }
            for(var i = 0; i < dt.length; i++) {
                var obj = dt[i];
                var request = new sql.Request(connection);
                request.stream = true;
                request.input('branchid', sql.Int(), obj.branch_id);
                request.input('picklistid', sql.Int(), obj.picklist_id);
                request.input('scanned',sql.Int(),obj.scanned);
                request.input('expected', sql.Int(),obj.expected);
                request.input('poscode', sql.VarChar(),obj.poscode);
                request.input('label', sql.VarChar(), obj.label);
                request.input('dt', sql.VarChar(), obj.dt);
                request.execute('WAREHOUSE_InsertPiPackData');
                request.on('done', function(returnValue) {
                    updated = updated + returnValue;
                    console.log(updated);
                });
            }
            res.send("[{\"ReturnValue\":" + updated + "}]");
        });

    }

I am sending in 4 lines of results and my console.log(updated) counts up as it should for each line, e.g 1,2,3,4

However the res.send result for updated is always 0.

Could anyone please explain why this is happening? My var updated is outside of my loop and this is getting updated correctly, however when the loop is finished it seems to get reset to 0?

returnValue == @@rowcount from the stored procedure

3 Answers 3

2

request is async so

res.send("[{\"ReturnValue\":" + updated + "}]");

gets executed even before you get the callback on request as JS doesn't wait for the callback and executes the next line. What you can do is use a counter and place your res.send inside for loop.

SendCompleted : function(req,res)
    {
        var updated = 0;
        var params = req.params.all();
        var dt = JSON.parse(decodeURI(params.id));
        var connection = new sql.Connection(testrmis, function (err)
        {
            if (err) {

            }
            var count = dt.length;
            for(var i = 0; i < dt.length; i++) {
                var obj = dt[i];
                var request = new sql.Request(connection);
                request.stream = true;
                request.input('branchid', sql.Int(), obj.branch_id);
                request.input('picklistid', sql.Int(), obj.picklist_id);
                request.input('scanned',sql.Int(),obj.scanned);
                request.input('expected', sql.Int(),obj.expected);
                request.input('poscode', sql.VarChar(),obj.poscode);
                request.input('label', sql.VarChar(), obj.label);
                request.input('dt', sql.VarChar(), obj.dt);
                request.execute('WAREHOUSE_InsertPiPackData');
                request.on('done', function(returnValue) {
                    count--;
                    updated = updated + returnValue;
                    console.log(updated);
                    if(count == 0) res.send("[{\"ReturnValue\":" + updated + "}]");
                });
            }
        });

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, for the explanation and a good recommendation for a solution, exactly what I was after :-)
1

Try for this:

May be Async problem:

for(var i = 0; i < dt.length; i++) {    
//Your logic    
  if(i=== dt.length){
     res.send("[{\"ReturnValue\":" + updated + "}]");
  }    
}

Comments

1

This is because at the time you do request.send, the value of updated is not incremented. This is because request.execute is asynchronous and done handler will be invoked after the res.send has been executed.

I would recommend a promise library (example, q). You can combine the promises and then use Q.all to do req.send when all the promises are done.

See more details here

1 Comment

Thanks for the suggestion, I can see the benefit of the library but feel it would be overkill in my case

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.