【发布时间】:2017-12-11 17:03:39
【问题描述】:
在我的路由文件中,我有一个条件,即外部函数检查两个值是否相等,obj.id === getId 并根据true 或false 呈现视图或阻止用户。
我尝试将函数checkUser 用作回调,它获取传递给它的值,但我无法将true 或false 的结果返回到我的路由文件。
路线文件
const express = require('express')
const router = express.Router()
const fcs = require('../routes/functions') //Global functions file
router.get('/:id', fcs.isLoggedIn, function(req, res, next) {
getId = req.params.id
db.con.query('SELECT * FROM employees where id=?', getId, function(err, results, callback) {
if (err) {
//some code
return
} else {
try {
//some code for getting results to obj{}
// here is the checking point that will use the callback
if (fcs.checkUser(obj.id, getId)) {
res.render('showuser')
} else {
//some code
}
} catch (err) {
//some code
}
}
})
})
全局函数文件
const express = require('express')
const router = express.Router()
module.exports.checkUser = function checkUser(uid, id, callback) {
// it gets true or false with no problem
console.log(uid === id)
// Need to send the output of true or false back to the condition checker (route file)
callback(uid === id)
}
【问题讨论】:
标签: javascript node.js express callback