I have table like this:
- no
- alphabets
no table contains (1,2,3,4)
alphabets table contains (apple,ball,cat,dog)
This my js file:
var connection = require('../connection');
function Todo() {
this.get = function(res) {
connection.acquire(function(err, con) {
con.query('select * from no', function(err, result) {
for(var key in result) {
var no = result[key].number;
console.log(no);
con.query('select * from alphabets', function(err, result2) {
for(var key in result2) {
var alph = result2[key].alphabets;
console.log(no+"."+alph);
}
console.log('\n');
});
}
con.release();
res.send(result);
});
});
};
}
module.exports = new Todo();
I need to have the parent child relationship between the two queries no table as parent and alphabets as child.
My Expected Output at command prompt :
1.apple
1.ball
1.cat
1.dog
2.apple
2.ball
2.cat
2.dog
3.apple
3.ball
3.cat
3.dog
4.apple
4.ball
4.cat
4.dog
Output I got:
1
2
3
4
4.apple
4.ball
4.cat
4.dog
4.apple
4.ball
4.cat
4.dog
4.apple
4.ball
4.cat
4.dog
4.apple
4.ball
4.cat
4.dog
I'm wondering that why the first query runs asynchronously and completes it loop. How can I create a relationship between the two queries so queries run synchronously avoiding last value of no table to be repeated at alphabets table?