如何进行异步连接。查询并将值返回给主函数

时间:2019-05-11 17:11:26

标签: mysql node.js

您好,我有一个中间件来检查角色和权限, 我需要使sql查询是否有结果,然后返回主函数true

var jwtInfo = require('../../helpers/jwt-decode')


exports.roleCheck =   function (token, permissionName) {

    var roleId = jwtInfo.decodeJwt(token).role
    var email = jwtInfo.decodeJwt(token).email



     connection.query(`SELECT * FROM users 
    INNER JOIN roles  ON users.roleId=roles.id
    INNER JOIN role_has_permissions ON users.roleId=role_has_permissions.role_id
    INNER JOIN permissions ON role_has_permissions.permission_id=permissions.id 
    WHERE email='${email}' AND roleId=${roleId} AND permissions.name='${permissionName}'
    `, function (error, results, fields) {

               if (error) return console.log(error)
               if (results.length > 0) {

              console.log(results)
                 return isAuthenticated=true
                //   console.log(isAuthenticated)
               } else {

               return isAuthenticated = false
                  // console.log(isAuthenticated)
               }

           })  

return isAuthenticated
}

我总是得到不确定的值

0 个答案:

没有答案