Tuesday, 4 April 2017

Select data from mysql and pass to the module and then the ejs engine page

So far i had done is that a dashboardController.js and db_functions.js is created. dashboardController.js render to all the views i will create later and db_functions will contain all the database queries etc.

dashboardController.js

var db_functions = require('./db_functions');
module.exports = function(app){

app.get('/services',function(req,res){

var data =db_functions.getServices(3);

console.log(data);   //always show undefined
res.render('services' , {services_data:data});

});
}

db_function.js

var getServices= function(a){

var query = "Select * FROM services  WHERE admin_id = ?" ;
connection.query(query,a, function(error,row,fields){
        if(!!error){
            console.log(a);
            console.log('here is error in query');
            return row;

        }else{

            var result = JSON.stringify(row);
            return result;
        }
    });
};

module.exports.getServices = getServices;

When i run it on browser using localhost:1122/services it show internal server error.The data variable inside the /services get function is always undefined . I think that the error is just because of the sync that the service view renders first and then databaes query is executed. Please help me regarding this issue.



via Bilal Mubeen

No comments:

Post a Comment