1

I would like to have multiple queries in a single router method as follows in my index.js file,

router.get('/users', function(req, res) {
var db = req.db; 
var users = db.get('users'); 
var col_name=req.query.colname; 
var col_value=req.query.colvalue; 
var query={}; 
query[col_name]=col_value;    
console.log(col_name);     
console.log(col_value);    
console.log(query);

//using this i would like to populate my dropdown list

users.distinct('symbol',{limit: 10000},function(e, syms){      
res.send('users', {      
title: 'usersSym',      
'usersSym': syms         
 });
 });

// using this I would populate a table in html

users.find(query,{limit: 10000},function(e, docs){
res.render('users', { 
title: 'Users',
'users': docs  
});
});
});

And in my .ejs file I'm trying to do the following :

<html>
<head>
 //drop down list populating with first query
<select id="selected" name="colvalue" >
<option value="">--Select--</option>
<% usersSym.forEach(function(usersym) { %>
<option value="<%= usersym.symbol %>"><%= usersym.symbol %></option>
<% }); %>
</select>
//Table populating with second query
<table >
<tr >
<th>Symbol</th>
<th>Order_id</th>
</tr>
<tr>
<% users.forEach(function(user) { %>
<td ><%= user.symbol %></td>
<td><%= user.order_id %></td>
</tr>
<% }); %>
</table>
</body>
</head>
</html>

But no luck. Wondering whether I'm going in right direction or not. If not please guide me in right way.

sasidharan
  • 127
  • 1
  • 2
  • 10
  • Take a look at [this answer here](http://stackoverflow.com/questions/29825166/how-to-return-multiple-mongoose-collections-in-one-get-request/29825572#29825572). It might help you. – Rodrigo Medeiros May 14 '15 at 11:24

2 Answers2

1

You can only send data back to the client once. One way would be to do all those DB queries in a sequence, and then send all the data. Another might be to do it your way, check the status of all DB queries, and if all are done, then send the data.

juunas
  • 54,244
  • 13
  • 113
  • 149
1

// 1st fetch symbol users.distinct('symbol',{limit: 10000},function(e, syms){ // 2nd fetch [users] users.find(query,{limit: 10000},function(e, docs){ res.render('users', { usersSym: syms, users: docs } ); }); }); // P.S. do not forget to check on error on each callback

Dilan
  • 90
  • 9