loopback icon indicating copy to clipboard operation
loopback copied to clipboard

loopback.getCurrentContext() seems to return null when MySQL is used

Open diasalvatore opened this issue 12 years ago • 113 comments

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?

diasalvatore avatar Nov 27 '14 16:11 diasalvatore

I am using almost precisely the same code, also extracted from #569. It works okay for me. Where are you placing the code?

doublemarked avatar Nov 27 '14 21:11 doublemarked

In my server.js.

I am using loopback 2.8.2, node 0.10

diasalvatore avatar Nov 27 '14 22:11 diasalvatore

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.

doublemarked avatar Nov 27 '14 22:11 doublemarked

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?

diasalvatore avatar Nov 28 '14 07:11 diasalvatore

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?

doublemarked avatar Nov 28 '14 07:11 doublemarked

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

diasalvatore avatar Nov 28 '14 07:11 diasalvatore

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

doublemarked avatar Nov 28 '14 08:11 doublemarked

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!

diasalvatore avatar Nov 28 '14 08:11 diasalvatore

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

diasalvatore avatar Nov 28 '14 11:11 diasalvatore

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.

doublemarked avatar Nov 28 '14 11:11 doublemarked

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!)

diasalvatore avatar Nov 28 '14 12:11 diasalvatore

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());

serkanserttop avatar Nov 28 '14 19:11 serkanserttop

@diasalvatore Your test project w/ a MySQL datastore indeed does fail for me also.

doublemarked avatar Nov 29 '14 08:11 doublemarked

This is most likely a duplicate of #809. There is a pending pull request #885 to fix the problem.

bajtos avatar Dec 01 '14 13:12 bajtos

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.

bajtos avatar Jan 14 '15 08:01 bajtos

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 avatar Mar 11 '15 12:03 viveke10

@viveke10 please provide an app that we can use to reproduce the problem locally on our machines, see wiki instructions.

bajtos avatar Mar 11 '15 18:03 bajtos

I cannot provide you app because its client project of our company

viveke10 avatar Mar 12 '15 04:03 viveke10

@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.

bajtos avatar Mar 12 '15 08:03 bajtos

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.

srguiwiz avatar May 21 '15 15:05 srguiwiz