1

I am trying to build a nodejs app with mysql and what I want is that my controllers and messages that I send should be in separate file as shown below

this is my auth controller file

exports.signup = (req, res) => {
  try {
    const { name, username, email, phone_number, gender } = req.body;
    const payload = [name, username, email, phone_number, gender];
    connection.query(signupUserQuery, payload, (error) => {
      if (error) {
        return errorMessage('Signup Failed');
      } else {
        return createdMessage('Signup SuccessFul');
      }
    });
  } catch (error) {
    return errorMessage('Signup Failed');
  }
};

and this is my messages file where I keep my messages

module.exports = {
  createdMessage: function (message) {
    return res.status(201).json({
      isError: false,
      message: message,
    });
  },
  errorMessage: function (message) {
    return res.status(500).json({
      isError: true,
      message: message,
    });
  },
};

So what I want is I want to return this function whenever controller is executed but the problem is when I try to do this I got error as res is not defined , So is there any way to use res in this messages.js File

What I have tried is I send res object from controller and that works but that is repetitive and I do not want to repeat myself

And one more thing I write my queries in separate file link this

insert into tbl_user (name,username,email,phone_number,gender) values (?,?,?,?,?)

but here the problem is I have to put question marks according to fields I require so is there any way to do that in single question mark ?

Bhumit 070
  • 416
  • 4
  • 12
  • In your signup function, you need to read this [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) because that function is not returning anything. Your return statements are just going back into the callback function. I don't understand the rest of your question. – jfriend00 Mar 17 '21 at 17:59
  • @jfriend00 if I send res object like this from my controller return createdMessage(res, 'Signup SuccessFul'); it shows something like this and return the message https://prntscr.com/10oempy , but i do not want to send res object everytime I call the messages that is why if I can have res object seperated from express that would be great – Bhumit 070 Mar 17 '21 at 18:03
  • 1
    Sorry, but I don't understand what that means. If you want to use `res` in a function in some other file, you have to pass it to that function as an argument because `res` is a transient object, different for every request. – jfriend00 Mar 17 '21 at 18:38
  • @jfriend00 i know that but i do not want to do that isn't it possible to extract res from express directly to use – Bhumit 070 Mar 17 '21 at 19:15
  • If you want to use 2 controllers as a single point without sending `res` and `req` objects, you should merge these two controllers into a single controller. But sending your `res` and `req` objects to another controller is not a bad practice. – Murat Colyaran Mar 17 '21 at 19:21
  • @Murat Colyaran no these are not different controllers , messages are simple function which needs to use response object , messages are not controllers – Bhumit 070 Mar 17 '21 at 19:22
  • 1
    I can't honestly tell what you're trying to ask for. If you want to use `res` in a function in another module, you HAVE to pass it to that function. There is no other way. If you can make your function so that it does not use `res` directly by grabbing whatever the function needs and passing those arguments directly such that you can just pass it data and have it return data, then you don't need to pass it `res`, but that is the only way. – jfriend00 Mar 17 '21 at 23:20
  • 1
    There is no such thing as "extract res from Express". `res` is an object that came from the underlying http server and Express modified with both methods and data. For example, `res.send()` is an Express added method. There is no extraction. You either pass `res` itself or you grab properties from `res` and pass just those properties to your functions. If your `errorMessage()` function wants to actually send a response to the http request, you HAVE to pass `res` to it - there is no other way with or without Express. – jfriend00 Mar 17 '21 at 23:23

1 Answers1

0

You can use it like this.

This is your messages.js file.

function createdMessage (message) {
  return {
      isError: false,
      message: message
  }
}

function errorMessage (message) {
  return {
      isError: true,
      message: message
  }
}



module.export = {
  createdMessage,
  errorMessage
}

This is auth controller.js

const { createdMessage, errorMessage } = require("./messages.js");
exports.signup = (req, res) => {
  try {
    const { name, username, email, phone_number, gender } = req.body;
    const payload = [name, username, email, phone_number, gender];
    connection.query(signupUserQuery, payload, (error) => {
      if (error) {
        return res
        .status(400)
        .send(errorMessage('Signup Failed'));
      } else {
        return res
        .status(201)
        .send(createdMessage('Signup SuccessFul'));
      }
    });
  } catch (error) {
        return res
        .status(400)
        .send(errorMessage('Signup Failed'));
  }
};
Murat Colyaran
  • 2,075
  • 2
  • 8
  • 27
  • i tried this and this works well but is there any way so that i do not have to write res.status(statuCode) every time – Bhumit 070 Mar 18 '21 at 16:14
  • Sorry no. Status is a function of the `res` object. If you don't want to send res object to other functions. This is the best method. – Murat Colyaran Mar 18 '21 at 19:16