I am calling getIndex function in contoller from routes and fetchAll from model is getting data from database but how to store the data and send it to contoller and how to receive in contoller.
This is how i have connected the database
const mysql = require('mysql2');
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database: 'practice',
password: ''
});
module.exports = pool.promise();
model
const db = require('../database');
module.exports = class User {
fetchAll(){
db.execute('SELECT * FROM users')
.then(([rows,fieldData]) => {
console.log(rows); //giving the required data
})
}
}
controller
const User = require('../model/user');
exports.getIndex = (req,res,next) => {
const user = new User();
user.fetchAll();
res.render('index');
};