I'm making my debuts in node.js. So I'm trying to get some data from database to display it into a view. In the model javascript file, I'm trying to make a connection to the database, after that I'm exporting a function to retrieve all the records from the table.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '',
user : '',
password : '',
database : ''
});
//Connecting to database.
connection.connect();
//This a query to get all the records that we will show in the view.
module.exports = {
getAllrecords: function(){
connection.query('SELECT * from orders', function(err, rows, fields) {
if (err) throw err;
var response = JSON.stringify(rows);
return response;
});
}
}
In my index layout, I'm trying to display the records, but I'm getting an undefined result.
var express = require('express');
var router = express.Router();
var database = require('../database');
var records = database.getAllrecords();
console.log(records);
So what I'm doing worng ?