Gunjan Gunjan - 1 year ago 130
Jade Question

How to send all rows of table to Jade using node.js

How do I send all rows of the original console value to Jade using node.js? This code is sending only 1 row of the WCF service to Jade.

var response = result['s:Envelope']['s:Body'] [0].OrderReportResponse[0].exeReport[0];
var marketOrder = response['a:MarketOrder'];
for (var i = 0; i < marketOrder.length; i++) {
var date = marketOrder[i]['a:SendingTime'];
var time = marketOrder[i]['a:TransactTime'];
var buyorSell = marketOrder[i]['a:Side'];
var orderQty = marketOrder[i]['a:OrderQty'];
var exchange = marketOrder[i]['a:Exchange'];
var contractName = marketOrder[i]['a:ContractName'];
var price = marketOrder[i]['a:Price'];
var filled = marketOrder[i]['a:mFile'];
var account = marketOrder[i]['a:Account'];
var wrkQty = marketOrder[i]['a:WrkQty'];
var execute = marketOrder[i]['a:ExeQty'];
var undQty = marketOrder[i]['a:UndQty'];
var type = marketOrder[i]['a:OrdType'];
var modifier = marketOrder[i]['a:Modifier'];
var trigPrice = marketOrder[i]['a:TrigPrice'];
var status = marketOrder[i]['a:Status'];
var avgPrice = marketOrder[i]['a:AvgPrice'];
var tif = marketOrder[i]['a:TimeInForce'];
var route = marketOrder[i]['a:Route'];
var originator = marketOrder[i]['a:Originator'];
var currentUser = marketOrder[i]['a:CurrentUser'];
var algoName = marketOrder[i]['a:AlgoName'];
var parentId = marketOrder[i]['a:ParentId'];
var tTOrderId = marketOrder[i]['a:TTOrderId'];
var ExchOrderId = marketOrder[i]['a:exchOrderId'];
//console.dir(JSON.stringify(date, time, buyorSell, orderQty, exchange, contractName, price, filled, account, wrkQty, execute, undQty, type, modifier, trigPrice, status, avgPrice, tif, route, originator, currentUser, algoName, parentId, tTOrderId, ExchOrderId));
res.render('aaa', {title: date, title1: time, title2: buyorSell, title3: orderQty, title4: exchange, title5: contractName, title6: price, title7: filled, title8: account, title9: wrkQty, title10: execute, title11: undQty, title12: type, title13: modifier, title14: trigPrice, title15: status, title16: avgPrice, title17: tif, title18: route, title19: originator, title20: currentUser, title21: algoName, title22: parentId, title23: tTOrderId, title24: ExchOrderId});
return res.send();

Answer Source

You have two major errors. First, in Express, you can only render and send a response once per view. Second, your return call causes the loop to immediately exit after the first attempt, so it only returns the first row. If not for the return statement. nesting these calls in a loop will cause the last row to render.

Avoid nesting a render call or a response send inside a loop — or even have a loop inside your Express view at all. There's no need to send 25 local variables to your Jade template; just send the marketOrder response object to the Jade template, and iterate on the object inside the template to append all rows to your results.

Your Express route code should be something like this:

var response = result['s:Envelope']['s:Body'][0].OrderReportResponse[0].exeReport[0];
var marketOrder = response['a:MarketOrder'];
res.render('aaa', {orders: marketOrder});
return res.send();

Inside your Jade template, I'd recommend using a mixin like this to iterate through rows and log the data to the console. Instead of hardcoding a tr value for each column, I included all the names as an array inside the mixin to iterate through. To keep things more DRY, I added a conditional that allows the mixin to be used for the thead row as well.

mixin tableRow(data)
        var cols = ['SendingTime','TransactTime','Side','OrderQty',

            each col in cols
                  if data == 'head'
                        td= data['a:' + col]

Then, within the HTML of your Jade file, iterate through the marketOrder array to append rows to your table and log that row's respective data server-side as below:

            each row in marketOrder
                 - console.log(JSON.stringify(row))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download