Always got null context in myModel.js file
Description/Steps to reproduce
I didn't get context using const context = LoopBackContext.getCurrentContext({bind: true}); I always get context null in MyModel.js file.
module.exports = function(Player) {
const LoopBackContext = require('loopback-context');
const app = require('../../server/server');
Player.GetPlayerByID = (ctx, callback) => {
const context = LoopBackContext.getCurrentContext({bind: true});
const currentUserResult = context && context.get('currentUserResult');
> console.log('currentUser.username: ', currentUserResult); // null
};
How my middleware as below:
module.exports = () => {
return function VerifyUserAccess(req, res, next) {
const token = req.header('Authorization');
const username = req.header('username');
const app = require('../../server/server');
const MSG = require('../../server/constant/message').MESSAGES;
const Users = app.models.users;
const now = new Date();
const LoopBackContext = require('loopback-context');
const loopbackContext = LoopBackContext.getCurrentContext({bind: true});
if (!username || !token) {
app.HandleError({message: MSG.INVALID_TOKEN, statusCode: 401}, err => { return res.status(401).json(err); });
} else {
Users.findOne({where: {username: username, accesstoken: token}}, function(err, userResult) {
. . .
. . .
else {
if (loopbackContext) {
console.log('\n\n\n\n\n ------ loopback context found: \n\n\n\n');
loopbackContext.set('currentUserResult', userResult);
}
next();
}
});
} else {
app.HandleError({message: MSG.TOKEN_EXPIRED, statusCode: 401}, err => { return res.status(401).json(err); });
}
}
});
}
};
};
And my middleware.json as below:
`{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"loopback-context#per-request": {
"params": {
"enableHttpContext": true
}
},
"./middleware/authorization": {},
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
},
"helmet#xssFilter": {},
"helmet#frameguard": {
"params": {
"action": "deny"
}
},
"helmet#hsts": {
"params": {
"maxAge": 0,
"includeSubdomains": true
}
},
"helmet#hidePoweredBy": {},
"helmet#ieNoOpen": {},
"helmet#noSniff": {},
"helmet#noCache": {
"enabled": false
}
},
"session": {},
"auth": {},
"auth:after": {
"./middleware/verify_user_access": {},
},
"parse": {},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {},
"final": {
"loopback#urlNotFound": {}
},
"final:after": {
"./middleware/log-error": {},
"strong-error-handler": {
"params": {
"debug": true,
"log": true
}
}
}
}
`
Please do let me know if I missing anything here.
Link to reproduction sandbox
Expected result
Additional information
Me too, LoopBackContext.getCurrentContext().get() In node version 10.1.0 alway return null. Switch to version 10.5.0 return normal;
There were a few bugs with earlier releases of Node 10 with how objects, arrays were treated, unexpected returning null / undefined. @NaimishKher95 does @FengYuHe 's suggestion work for you?
I am experiencing the same errors any update
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.