2
votes

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?

1

1 Answers

2
votes

You can run functions with callbacks synchronously using SynJS. I refactored your code a bit, and here is a working version:

global.SynJS = global.SynJS || require('synjs');
var mysql      = require('mysql');

var connection = mysql.createConnection({
    database : 'tracker',
    user     : 'tracker',
    password : 'tracker123'
});


var myFunction1=function( mysql, connection ) {

    connection.connect(function(err) {
        SynJS.resume(_synjsContext);
    });
    SynJS.wait();


    var result;
    connection.query('select * from no', function(err, res) {
        result = res;
        SynJS.resume(_synjsContext);
    });
    SynJS.wait();

    for(var key in result)
    {
        var no = result[key].number;
        console.log(no);
        var result2=null;
        connection.query('select * from alphabets', function(err, res2) {
            result2 = res2;
            SynJS.resume(_synjsContext);
        });
        SynJS.wait();

        for(var key in result2)
        {
            var alph = result2[key].alphabets;
            console.log(no+"."+alph);
        }
        console.log('\n');

    }
};

SynJS.run(myFunction1,null, mysql, connection, function (ret) {
    console.log('done');
});