title: How to send response from class object promise to controller in nodeJS
body: <p>I have a question.
  I am using a NodeJS app divided in three main parts</p>
  <ol>
  <li><p>Data access object
  Here, I created a function that make the direct interaction with the database only in this file.</p>
  <p>const { response } = require('express');
  const db = require('../connection/db.js');</p>
  <p>/* Export */
  module.exports = {
  // Insert User
  insertUser: (parameters) =&gt; {
  const sqlStatement = 'INSERT INTO users SET ?';
  return new Promise((resolve, reject) =&gt; {
  db.query(sqlStatement, parameters, (error, response) =&gt; {
  if (error) {
  return reject(error);
  }
  return resolve(response);
  })
  })
  }
  }</p>
  </li>
  <li><p>Object class
  In this class I will make the validation, and create the method with arguments, then implement the data access object <code>insertUser</code>.</p>
  <pre><code> const db = require('../dataAccessObject/dao.js');
  </code></pre>
  <p>class User {
  constructor (fname, lname, age, email, password, avatar, gender) {
  this.fname  =  fname;
  this.lname  =  lname;
  this.age  =  age;
  this.email  =  email;
  this.password  =  password;
  this.avatar  =  avatar;
  this.gender  =  gender;
  }</p>
  <pre><code> /* Methods */
   createUser() {
       db.insertUser({
           'firstName'     : this.fname,
           'lastName'      : this.lname,
           'age'           : this.age,
           'email'         : this.email,
           'password'      : this.password,
           'userAvatar'    : this.avatar,
           'gender'        : this.gender
       })
       .then(() =&gt; {
           console.log(this);
           return this;
       })
       .catch(error =&gt; {
           if (error.code == 'ER_DUP_ENTRY' &amp;&amp; error.errno === 1062) throw new Error('This email is allready in use')
           else throw error;
       })
   }
  
   /* Validations */
  </code></pre>
  <p>}</p>
  <p>module.exports = User;</p>
  </li>
  <li><p>Controller</p>
  <p>const newUser = new User(&quot;fname&quot;, &quot;lname&quot;, 22, &quot;ema2121il&quot;, &quot;password&quot;, &quot;avatar&quot;, &quot;gender&quot;);
  newUser.createUser();
  res.send(newUser)</p>
  </li>
  </ol>
  <p>I need to find a way to pass the validations errors, or the duplicate error from the point 2 (Object class) to the controller.</p>
  <p>Please how can I do this?</p>