I'm pretty new to using Express.js and I have an issue returning the data from the database query. The goal is to get user info from the table as JSON inside userController but only thing I get is undefined. I tried many different things but can't get my head around how to return the value from the method.
db.js
const mysql = require('mysql2');
var con = mysql.createPool({
//db data here
});
module.exports = con;
userController.js
var User = require("../models/userModel");
exports.registerUser = function(req, res){
if(req.method == 'POST'){
var username = req.body.username;
var email = req.body.email;
//doing checks
var u1 = new User(username, email);
console.log(u1.getInfo());
}
}
userModel.js
const db = require('../config/db');
module.exports = class User{
//constructor here
getInfo(){
try{
var query = "SELECT * FROM users";
db.query(query, function(err, data){
return JSON.stringify(data);
});
}
catch(err){
return err;
}
}
}