Please break down the event path of a Callback Function into an ORM file

162
August 14, 2017, at 4:38 PM

I was tasked with creating a small application using Object Relational Mapping (ORM) for class. It's a fairly simple application, that takes client side input, places it into the database and also displays it back to the client.

However, I am having a hard time understanding the breakdown in what is happening as we pass code between 3 files. There is a burgers-controller.js file which requires a burgers.js file which requires the orm.js file. For anyone who wants to see the full code you can find it on my Github.

I get the reasoning and benefits of using an ORM and modularizing everything but I need someone to break down what is happening inside the code and in what order.

So in burgers-controller.js file we have the following code.

router.get("/", (req, res)=>{
  burgers.selectAll((result)=>{
    let handlebarsObj = {
      burgers: result
    }
    res.render("index", handlebarsObj)
  });
})

Then inside burgers.js we have the following code.

let burgers = {
  selectAll: (callback)=>{
    orm.selectAll("burgers",  (result)=>{
      callback(result);
    })
  }
}

And finally in orm.js we have.

let orm = {
  selectAll: (tableName, callback)=>{
    let queryString = "SELECT * FROM ??"
    connection.query(queryString, tableName, (err, data)=>{
      if (err){console.error("ERROR: " + err.stack)}
      callback(data)
    })
  }
}

The ORM file is connected to a MySQL database.

Would you explain what is happening here, and in what order?

Answer 1

I think I got it, although its a shot in the dark since there are no indication of what the actual error is, in your description. Please forgive me if I mistakes.

The callback from burgers-controller.js which is sent to burgers.selectAll( is executed inside burgers.js. When its executed inside burgers.js and not within burgers-controller.js it wont have access to res object, which is only present in burgers-controller.js.

We can solve the problem using promises. I am going to use native one here, but you are free to use any other package (eg: bluebird):

// burgers-controller.js
router.get('/', (req, res)=> {
    burgers
        .selectAll()
        .then((result) => {
            let handlebarsObj = {
                burgers: result
            }
            res.render('index', handlebarsObj)
        })
})
// burgers.js
let burgers = {
    selectAll: () => {
        return new Promise((resolve, reject) => {
            orm.selectAll('burgers', (result) => {
                resolve(result)
            })
        })
    }
}

You can use promises everywhere are deal with failures more gracefully:

// burgers-controller.js
router.get('/', (req, res)=> {
    burgers
        .selectAll()
        .then((result) => {
            let handlebarsObj = {
                burgers: result
            }
            res.render('index', handlebarsObj)
        })
        .catch((err) => {
            let errMsg = {
                message: err.message,
                error: err
            }
            res.render('index', errMsg)
        })
})
// burgers.js
let burgers = {
    selectAll: () => {
        return new Promise((resolve, reject) => {
            orm
                .selectAll('burgers')
                .then((result) => {
                    resolve(result)
                })
                .catch((err) => {
                    reject(err)
                })
        })
    }
}
// orm.js
let orm = {
    selectAll: (tableName) => {
        return new Promise((resolve, reject) => {
            let queryString = "SELECT * FROM ??"
            connection.query(queryString, tableName, (err, data)=>{
                if (err) {
                    console.error("ERROR: " + err.stack)
                    reject(err)
                } else {
                    resolve(data)
                }
            })
        })
    }
}
READ ALSO
Elastic Beanstalk .ebextensions complete documentation?

Elastic Beanstalk .ebextensions complete documentation?

I'm new to beanstalk, and even AWSI've successfully deployed a Node/Express app which serves a React/Redux SPA

213
Passing array of objects from EJS template to Javascript function?

Passing array of objects from EJS template to Javascript function?

I have a basic Nodejs application and when a user hits a certain route, a number of records are retrieved from the database in the route and passed to my EJS template as an array

230
Few newbie react-native questions

Few newbie react-native questions

I'm new to react and react-native (and pretty much everything in general

146
Exporting a Mongoose connection that becomes available in an async callback?

Exporting a Mongoose connection that becomes available in an async callback?

I'm new to coding in Nodejs and I've created a DB connection through mongoose, then exporting the database object into my main app

159