aUtil.js
module.exports = {
successTrue: function(data) {
return { data: data, success: true };
},
isLoggedin: async (req, res) {
//decoded token in req header
//if decoded token success,
res.json(this.successTrue(req.decoded));
}
}
that function call in test.js
router.get('/check', aUtil.isLoggedin, async (req, res) => { ... })
I want to use the function above in that function.
But I keep getting errors.
ReferenceError: successTrue is not defined
I tried many ways.
'res.json(successTrue( ... )'
Use this
:
module.exports = {
successTrue: function() {
return { foo: 'bar' }
},
isLoggedin: function() {
console.log(this.successTrue())
}
}
You're exporting an object, so this
refers to itself.
Also make sure you're binding aUtils
if you're using it as middleware like so:
router.get('/check', aUtil.isLoggedin.bind(aUtil), async (req, res) => { ... })
Try this :
const aUtil = {
successTrue: function() { //return json obj },
isLoggedin: function() {
res.json(aUtil.successTrue( ... ));
}
}
module.exports = aUtil;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.