0

I have a file called admin.js, which has a function that performs the action of returning a BOOLEAN based on if the ROLE column in the table is set to ADMIN. the problem is, I am trying to get a hold of this boolean in another function.

admin.js function

  const adminCheck = async (req, res, callback) => {
    console.log(“one”)
    await UtilRole.roleCheck(req, res, ‘ADMIN’, (response) =>  {
        if(response) {
            console.log(“one: true”)
             return true
         } else {
            console.log(“one: false”)
            return false
            //  return callback(null, false)
         }
     })
    }
    module.exports = {
    adminCheck
    }

in this above function, it is returning true or false depending on what the user is, but im not sure how to get that value into the index.js function, which is below:

then in my index.js, I have this:

 router.get(‘/viewRegistration’, auth.ensureAuthenticated, function(req, res, next) {

      const user = JSON.parse(req.session.passport.user)
      var query =  “SELECT * FROM tkwdottawa WHERE email = ‘” + user.emailAddress + “’”;
        console.log(“EMAIL ADDRESS user.emailAddress: ” + user.emailAddress)

        ibmdb.open(DBCredentials.getDBCredentials(), function (err, conn) {
          if (err) return res.send(‘sorry, were unable to establish a connection to the database. Please try again later.’);
          conn.query(query, function (err, rows) {
            if (err) {
            Response.writeHead(404);
          }

           res.render(‘viewRegistration’,{page_title:“viewRegistration”,data:rows, user});

          return conn.close(function () {
            console.log(‘closed /viewRegistration’);

          });
          });
        });
      //res.render(‘viewRegistration’, { title: ‘Express’, user });
    })

now my question is, how would I call the returned result of either true, or false, in my /viewregistration function in index.js?

Gianluca
  • 900
  • 8
  • 27
  • Looks like a duplicate of this very popular question https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – elclanrs Apr 07 '21 at 17:04
  • @elclanrs that won't help as it has very different language and it is reading files – Gianluca Apr 07 '21 at 17:08
  • I would recommend reading through the answers in that question because this seems more of a conceptual misunderstanding than anything else. You cannot return from an async function. – elclanrs Apr 07 '21 at 17:11
  • @Gianluca I couldn't see which part of the code are you calling the `adminCheck`function in the `index.js`? – Murat Colyaran Apr 07 '21 at 18:15

1 Answers1

1

As far as I understand from your question, you need a middleware. If I were you, I would use middleware like this.

const UtilRole = require('path/UtilRole')

function adminCheck() {
  return (req, res, next) => {

    console.log('one');

    UtilRole.roleCheck(req, res, 'ADMIN', (response) => {
      if (response) {
        console.log('one: true');
        req.isAdmin = true; // This is how you get the boolean
        return next(); // With next() you left from the middleware
      } else {
        console.log('one: false');
        req.isAdmin = false; 
        return next();
      }
    })
  }
}


module.exports = {
  adminCheck
}

And you index.js will look like this

const { adminCheck } = require('path/admin');

router.get('/viewRegistration', adminCheck, auth.ensureAuthenticated,  function(req, res, next) {

  const isAdmin = req.isAdmin; // Voila, here yours boolean from admin.js

  ...
})
Murat Colyaran
  • 2,075
  • 2
  • 8
  • 27