I am newbie in node.js and I'm sorry for my silly question.
I want to separate and organize my app for easy working with files. For this reason I create a module to do my mysql database there but I have problem with module.I can't return module data to use in my main js file app.js
console shows undefined and browser shows nothing
here is my code
App.js
var express = require('express'),
mysql = require('mysql'),
bodyParser = require('body-parser');
var app = express();
// app.set('view engine', 'jade');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: 'database'
});
var port = process.env.PORT || 8080;
var User = require('./models/userModels');
var bookRouter = express.Router();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
bookRouter.route('/books')
.post(function (req, res) {
// console.log(book);
// res.send(book);
})
.get(function (req, res) {
res.send(User.allUsers); // <--- shows nothing
console.log(User.allUsers); //<--- returned undefined
});
app.use('/api', bookRouter);
app.get('/', function (req, res) {
res.send('welcome to my API');
});
app.listen(port, function () {
console.log('Running on PORT via gulp ' + port);
});
userModels.js
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "luxa"
});
module.exports = {
allUsers: con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM users", function (err, result, fields) {
if (err) throw err;
// console.log(result); // return result correctly
// return result;
callback(err, result); // Error
});
}),
};
what's my problem?
userModels?userModule.jsin fact that isuserModel.jsI edit the codeuserModel, but you importuserModels