loopback.getCurrentContext() seems to return null when MySQL is used
As in https://github.com/strongloop/loopback/issues/569 I was trying to set currentUser in context in order to retrieve it after in requests.
app.use(loopback.token()); // this calls getCurrentContext
app.use(loopback.context()); // the context is started here
app.use(function (req, res, next) {
if (!req.accessToken) return next();
app.models.User.findById(req.accessToken.userId, function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('No user with this access token was found.'));
res.locals.currentUser = user;
var loopbackContext = loopback.getCurrentContext();
if (loopbackContext) loopbackContext.set('currentUser', user);
next();
});
});
the problem is that loopback.getCurrentContext() returns null so it will not be set.
Why does this happen?
I am using almost precisely the same code, also extracted from #569. It works okay for me. Where are you placing the code?
In my server.js.
I am using loopback 2.8.2, node 0.10
All the same on my side, sorry. If you post a more complete code snippet I can do a more thorough comparison, if you like.
Here it is: http://pastebin.com/kiHwaVeh but it's almost the default server.js, thank you.
Have I to set something in configuration somewhere?
There is no special configuration for it that I have done.
Can I suggest you do an npm upgrade on your project to ensure related modules are not at an incompatible version?
I did it, nothing changed.
This is the getCurrentContext() function in loopback, ns is created, but ns.active is null so it returns null.
function createContext(scope) {
// Make the namespace globally visible via the process.context property
process.context = process.context || {};
var ns = process.context[scope];
if (!ns) {
ns = cls.createNamespace(scope);
process.context[scope] = ns;
// Set up loopback.getCurrentContext()
loopback.getCurrentContext = function() {
return ns && ns.active ? ns : null;
};
chain(juggler);
chain(remoting);
}
return ns;
}
I really don't know why it doesn't work, it seems that everyone uses this code snippet
I have built you a very simple project which implements this routine. Everything seems to work fine. Perhaps you can use it to find what's broken in your project: http://goo.gl/CCrbgM
Thank you, you were very kind. I did the same creating a new loopback application and it works either! How strange is that? Now I'll put all old files in this projects and hope. Thank you again!
Could you try what happens if you link your User model to a MySQL datasource?
Because if I do this, it returns null to me
I'm not set up right now with a MySQL datasource, but can try perhaps in a day or two. Sounds like you may have found the difference, though.
Finally, but it costed me one day. Here is your example with MySQL configured: https://www.dropbox.com/s/uobkp2x8bg610o6/contextTestBugged.zip?dl=0
I hope that MySQL is the cause (and if it is that someone will fix it!)
Just a suggestion, I did not test it, but could you try app.use(loopback.context()); app.use(loopback.token()); rather than app.use(loopback.token()); app.use(loopback.context());
@diasalvatore Your test project w/ a MySQL datastore indeed does fail for me also.
This is most likely a duplicate of #809. There is a pending pull request #885 to fix the problem.
The bug should have been fixed by #809 in [email protected], I am closing this issue as resolved. Please leave a comment if the problem is still present in the latest loopback version.
I have installed [email protected] but still gets currentUser: null , as mentioned below
var ctx1 = loopback.getCurrentContext(); var currentUser = ctx1 && ctx1.get('currentUser'); console.log('currentUser: ', ctx1); console.log('currentUser.username: ', currentUser); //console.log('ctx: ', ctx); // voila! if (ctx.instance) { ctx.instance.updated = new Date(); } else { ctx.data.updated = new Date(); } next();
@viveke10 please provide an app that we can use to reproduce the problem locally on our machines, see wiki instructions.
I cannot provide you app because its client project of our company
@viveke10 Have you read the instructions? We are not asking you to disclose your proprietary code, but to create a new small app that reproduces the problem.
- Fork loopback-sandbox
- Add the code required to reproduce your issue in the forked repository.
Before trying the sandbox, here a report I got same (it seems) problem, with MongoDB though, even as I have the latest versions everything, with supposed fix for MongoDB.
loopback.getCurrentContext() returning null because its ns.active is null .
I found in a beforeRemoteMyMethod method a loopback.getCurrentContext() still works. It is only once it gets to the corresponding myMethod, a custom method I wrote, that it is null.
Further I have checked, it isn't the namespace it is supposed to be. I am looking in the debugger where it gets to node_modules/loopback/server/current-context.js at method loopback.createContext and am doing a debugger console test with ns.get('mystuff') and what was there before isn't there. That seems to indicate not only that it isn't active, but it isn't the same namespace that it was in my beforeRemoteMyMethod.
Got current versions couple days ago, to be sure did slc update and npm update . Then from npm list | grep loopback I have
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
├─┬ [email protected]
├─┬ [email protected]
├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
node --version gives v0.12.3, but was same with v0.12.2.