1
\$\begingroup\$

What should I consider in order to refactor the following code ?

It seems very close to proudly become a Pyramid of Doom:

router.get('/:id', function(req, res) {
 getId = req.params.id
 obj = {}
 db.con.query('SELECT * FROM employees where id=?', getId, function(err, results) {
 if (err) {
 console.log('error in query')
 return
 } else {
 try {
 obj = {
 id: results[0].id,
 name: results[0].name,
 location: results[0].location
 }
 res.render('showuser')
 } catch (err) {
 res.status(500)
 res.render('error', { error: err, id: getId })
 }
 }
 })
});
asked Dec 8, 2017 at 10:42
\$\endgroup\$
1
  • \$\begingroup\$ Really doesn't look that bad, regardless consider using async / await for more succinct code. Presumably the DB lib you're using supports Promises. \$\endgroup\$ Commented Dec 9, 2017 at 0:51

2 Answers 2

1
\$\begingroup\$

Are we actually using the obj variable? It seems like you are not using it unless you were intending to pass to the render function. I don't think the try block is necessary. You can first check if results contains elements in it, so doing results[0].X will not return you an error since results[0] will never be undefined.

Here's an improved version:

router.get('/:id', function(req, res) {
 const getId = req.params.id;
 db.con.query('SELECT * FROM employees where id = ?', getId, function(err, results) {
 if (err || !results.length) {
 console.log('error in query');
 res.status(500).render('error', { error: err, id: getId });
 return;
 }
 var obj = {
 id: results[0].id,
 name: results[0].name,
 location: results[0].location
 };
 res.render('showuser', obj); // Assuming you're using obj to render
 });
});
answered Mar 10, 2018 at 2:36
\$\endgroup\$
0
\$\begingroup\$
router.get('/:id', function(req, res) {
 const getId = req.params.id
 db.con.query('SELECT * FROM employees where id=?', getId, function(err, results) {
 if (err) {
 console.log('error in query')
 return
 }
 try {
 const obj = {
 id: results[0].id,
 name: results[0].name,
 location: results[0].location
 }
 res.render('showuser')
 } catch (err) {
 res.status(500)
 res.render('error', { error: err, id: getId })
 }
 })
});

There are a few of things I changed

  • added const to getId now it may not interfere with other variables called getId in your module
  • moved definition of the obj inside the try block, since this is the only place you are using it as well as added const for the same reason
  • since you have return in your if (err) condition, the function will end there if it errors. Therefore, else is unnecessary here and was removed
answered Dec 9, 2017 at 23:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.