0

I am New To async/await coding in node js. I am trying to fetch data from mysql and then populate the result into an object. But i am not finding a way how to do it in controller.js file. My source code similar to this.

  1. My Router File
const express = require("express");
const controller = require("controller");
const router = express.Router();
router.route("/").get(controller.findAll);
  1. My Controller File
const model = require("model");
exports.findAll = async (req, res) => {
   
    const all = model.getAll((err, data) => {
        if (err) {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving data.",
            });
        }
        return data;
    });
    const user = model1.getUser((err, data) => {
        if (err) {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving user.",
            });
        }
        return data;
    });
    // somefunctionality(all, user);
    // res.send(result);
};
  1. My Model file
const con = require("./db");

// constructor
const Customers= function (customer) {
    this.id = customer.id;
    this.first_name = customer.first_name;
    this.last_name = customer.last_name;
    this.email = customer.email;
};
Customers.getAll = () => {
    const query = `SELECT * FROM doctors`;
    sql.query(query, (err, res) => {
      if (err) {
          console.log("error: ", err);
          result(null, err);
          return;
    }
    console.log("customers: ", res);
    result(null, res);
};

user model same as customer

Thanks for your help

3

1 Answer 1

1

The first thing I would do is covert your Customers.getAll method to return a promise:

model file

Customers.getAll = () => {
  const query = `SELECT * FROM doctors`;
  return new Promise((res, rej) => {
    sql.query(query, (err, data) => {
      if (err) {
        console.log("error: ", err);
        rej(err);
        return;
      }
    console.log("customers: ", res);
    res(data);
  })
};

(Note that you'll probably have to do something similar to the getAll method for your User model)

Now, in the controller, you can use try/catch to handle any errors.

*controller file

exports.findAll = async (req, res) => {
  try {
    const all = await model.getAll();
    const users = await model1.getUser();
    const result = somefunctionality(all, user);
    res.send(result);
  } catch(err) {
    res.status(500).send({
      message: err.message || "Some error occurred while retrieving data.",
    });
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Nick for your help. but i was trying to do like only useing async and await keyword.
So that depends a bit on whether the sql package has promises built-in. Otherwise, you're going to have to convert any callbacks to Promises because async await is just promises under the hood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.