Description: I do a mysql query to a table, and get back some rows
> connection.query(q2,function(err,rows){...}
lets say the rows looks like {id:",,,", time:"..." etc:"cc"}
Next for every row i query another table and get back some lets say coms based on the id of the row. there can be 0 - 100 rows depenting on the query
> for (var i = 0; i < rows.length; i++){
> connection.query(qc,function(err2,coms){
lets say the coms looks like this { rowid:",,,", something:"ddd" smthelse:"ff"}
there can also be 0 - 100 coms in every row
Question:
How can i add coms inside rows, so for every row i have the coms i got back from the table? i want something like this
> {id:"1", time:"..." etc:"cc" coms:{ rowid:",,,", something:"ddd"
> smthelse:"ff"}}
Allready tried:
rows[i].comms=coms; (not working)
rows[i].comms[j]=coms[j]; (not working)
rows[i].push(coms) (not working)
rows.comms=coms; (working but its not what i want. with this i get multiple times the same data)
the whole code i am using after the first query
connection.query(q2,function(err,rows){
// connection.release();
if(!err) {
// rows["coms"]=[];
for (var i = 0; i < rows.length; i++){
var qc="select * from comms where id='"+rows[i].id+"' order by com_id";
connection.query(qc,function(err2,coms){
if(!err2) {
// rows[i].cid=coms.com_id;
// rows[i].coms=[];
rows[i]="coms";
for (var j = 0; j < coms.length; j++){
// data[i][j]=coms[j];
// rows[i].coms[j]=coms[j];
// rows[i][j].coms=coms[j];
}
// data[i]=coms;
// rows[i]["coms"]=coms;
// console.log(rows);
}
});
}
// rows.push(data);
console.log(rows);
users[index].emit('data',rows);
}
connection.release();
});
connection.querycalls and then callsemit('data', rows)but when emit is called, some (most) of the queries have not yet had time to complete. As I write above, you need to use some way to handle the asynchronicity and collect the data you need before callingemit. I usually use promises for this task and really like the bluebird library.