Query response value getting passed from my model to my controller?

I am building a node js server with a mysql db. I am getting the proper response from my db, but my model is not properly sending the response to my controller, or my controller is not waiting for the async function to finish. response in my controller file comes back as undefined. Here is my code:

Model:

const sqlDb = require('../../db/index.ts');

module.exports = {
  getNProducts: (page = 0, n = 5) => {
      let offset = page * n;
      if (offset > 0) {
        sqlDb.query(`SELECT * FROM Products ORDER BY id DESC LIMIT ${n}, ${offset}`, (err, res, feilds) => {
            if (err) {
                return;
            } else {
                return res;
            }
        });
        } else {
        sqlDb.query(`SELECT * FROM Products ORDER BY id DESC LIMIT ${n}`, (err, res, feilds) => {
            if (err) {
                console.log(err);
                return;
            } else {
                return res;
            };
          });
        }
  }
}

Controller:

 const productModel = require('../models/productModel.ts');
    
    module.exports = {
        getProducts: async(req, res) => {
          let response = await productModel.getNProducts();
          res.send(response).status(200);
        }
    }

128 thoughts on “Query response value getting passed from my model to my controller?”

  1. Pingback: priligye
  2. Pingback: ivermectin 400mg

Leave a Comment