Sunday, 21 May 2017

how to select data from multiple tables in nodejs

I'm working on creating a Project Registration and have to select the data from multiple tables. Main table : Project ( primary-key : id ) Project_stage1 : contains foreign-key ( project_id ) relates to Project( id ) Project_stage2 : contains foreign-key ( project_id ) relates Project( id )

    'use strict';
    var mysql=require("mysql");
     function PROJECTS(connection,callback){
        var query="select * from ??";

    var table=["project"];
    query=mysql.format(query,table);

    connection.query(query,function(err,rows){
        if(err){
            console.log("error finding Change Type");
        }else{
               callback(null,rows);
        }
    });

}

function PROJECT_CREATE(connection,id,callback){

    var query="select * from ?? where ??=?";
    var table=["project_stage1","project_id",id];
    query=mysql.format(query,table);

    connection.query(query,function(err,rows){
        if(err){
            console.log("error finding Change Type");
        }else{
            //console.log(JSON.stringify(rows));
            //JSON.stringify(rows);
               callback(null,rows);
        }
    });
}

function PROJECT_ENGAGE(connection,id,callback){

    var query="select * from ?? where ??=?";
    var table=["project_stage2","project_id",id];
    query=mysql.format(query,table);

    connection.query(query,function(err,rows){
        if(err){
            console.log("error finding Change Type");
        }else{
            //console.log(JSON.stringify(rows));
            //JSON.stringify(rows);
               callback(null,rows);
        }
    });

}



module.exports.PROJECTS=PROJECTS;
module.exports.PROJECT_CREATE=PROJECT_CREATE;
module.exports.PROJECT_ENGAGE=PROJECT_ENGAGE;

I have to execute all the above three functions in which PROJECT function return JSON and with that JSON i have to add below funtions JSON data corresonding to their Id's and create a json as response like

{  projects:
     [{project1,createStage:[],engageStage:[]},
      {project2,createStage:[],engageStage:[]}
     ]
 }

Here is my controller

var registerprojectmodel=require("../model/RegisterProjectModel.js");

function REGISTER_PROJECT(router,connection){
     var self = this;
     self.handleRoutes(router,connection);
}

REGISTER_PROJECT.prototype.handleRoutes=function(router,connection){

    router.get("/GetProjects",function(req,res){

        registerprojectmodel.PROJECTS(connection,function(err,data){


       aggregate('Projects', data);



        });

          var results = {};
        function aggregate(name, data) {
            results[name] = data;

            if(results.Projects) {



                var totalProjects=Object.keys(results.Projects).length;

                for(var i=0; i<totalProjects;i++){
                    registerprojectmodel.PROJECT_CREATE(connection,i,function(err,data){
                        stages('create', data);
                    });
                    registerprojectmodel.PROJECT_ENGAGE(connection,i,function(err,data){
                        stages('engage', data);
                    });
                    registerprojectmodel.PROJECT_COMMUNICATE(connection,i,function(err,data){
                        stages('communicate', data);
                    });
                    registerprojectmodel.PROJECT_IMPLEMENT(connection,i,function(err,data){
                        stages('implement', data);
                    });
                    registerprojectmodel.PROJECT_MEASURE(connection,i,function(err,data){
                        stages('measure', data);
                    });

                    var stagesresults = {};
                    function stages(name, data) {
                        stagesresults[name] = data;
                        if(stagesresults.create && stagesresults.engage && stagesresults.communicate &&
                                stagesresults.implement && stagesresults.measure) {
                             //console.log(JSON.stringify(stagesresults));

                            results.Projects[i]=stagesresults;
                       }
                     }  

                }


                res.send(results);
             }

            }
    });

}
module.exports = REGISTER_PROJECT;

But i am only getting first function that is project data in response. Any solution regarding this will be very much appreciated.



via Manish Vyas

No comments:

Post a Comment