K
K
kote222016-12-28 10:25:59
JavaScript
kote22, 2016-12-28 10:25:59

The function is not executed to the end! what to do?

In general, if the essence is as follows:
there is a table with different types of socks, we look for all ids
in another table, we look for how many socks with this id were sold, we add up the sales of each sock, shove everything into an array, then json.
but alas, the second nested loop with the addition of the number of sales does not get into the array.
the array should be like this:
id - 1
sales - 50
but for now it's like this:
id - 1
sales - undefined

items_match();
    function items_match() {

        var connection = new Connection(config);
        connection.on('connect', function(err) {
           if (err) {
          console.log(err);
           } else {
request = new Request("SELECT * FROM dbo.items ", function(err, rowCount) {
res.send(JSON.stringify(item_arr));
})
var item_arr = new Array();
request.on('row', function(columns) {
var item_id = columns[0].value;
items.push(columns[0].value);
items.push(sales_math(item_id));
item_arr.push(items);
console.log(item_arr);
})
connection.execSql(request);
}
})  
}
function sales_math(id) {
var total = 0;
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log(err);
} else {
var request = new Request("SELECT * FROM dbo.Sales WHERE [email protected] ", function(err, rowCount, rows){
return total;
console.log("total: "+total);
});
request.on('row', function(columns) {
total =  Number(total)+Number(columns[12].value);
console.log(total);
});
connection.execSql(request);
}
});
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question