1

i'm using the following to build a web app: nodejs,express,handlebars and mysql. I'm trying to get query results from one file dbconnect.js to my app file app.js via the exports object. I have seen some suggestions to getting query results by using jade, but can this be accomplished with this tech stack?

Portion of dbconnect.js

exports.allBooks=function(req,res){
connection.connect(function(err){
    if(err){
        console.log('Database connection failed'+err.stack);
        return;
    }

    console.log('Connected to the db');
    connection.query("select Title from Books",function(err,result,fields){
        if(err) throw err;
        console.log(result);
        return result;
        connection.end();
    })
});
};

Portion of app.js

var mydb = require('./dbconnect.js');
...
...
app.post('/process',function(req,res){
var myresults=mydb.allBooks(req,res);
console.log(myresults);
res.redirect(303,'/thank-you');
});

myresults prints out as undefined. Thank you so much for any comments/answers/suggestions.

0 Answers0