Asked 30+ days ago
17 Aug 2017
Views 65
yogi

yogi posted

how to run two Mysql Query in one function with node js in Express Framework

i am dealing with a form for edit where i need to get data from table for parameter id

exports.edit= function(req,res){
  var id = req.params.id;
  var query = connection.query('SELECT * FROM data WHERE id = ?',[id],function(err,data)
  {
           res.render('admin/edit',{page_title:"Edit",raws:data});
         });
};


but form have drop down which also come from database so i want to use two query one for data edit fetch and second for the drop down list

  <form id="edit" method="post" action="/admin/edit/<%=data[0].id%>">
<select class="form-control" type="text" placeholder="" name="data1" required>
		                    		<option>Select Category</option>

</select>
</form>


so i can run two query in same function so i get the drop down value from database table and form value also from database table

===
what i tried

exports.edit= function(req,res){
  var id = req.params.id;
  var query = connection.query('SELECT * FROM dropdownoptionlist  ',[id],function(err,data)
  {
           res.render('admin/edit',{page_title:"Edit",raws:data});
         });
  var query = connection.query('SELECT * FROM data WHERE id = ?',[id],function(err,data)
  {
           res.render('admin/edit',{page_title:"Edit",raws:data});
         });
};

but in this case it goes to fetch only dropdownoptionlist only not from data
so how can i run two query and pass that parameter to template and render it properly

Post Answer