Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loosing context after external request #1177

Closed
umbrella-ivan-zhukov opened this issue Mar 5, 2015 · 10 comments
Closed

Loosing context after external request #1177

umbrella-ivan-zhukov opened this issue Mar 5, 2015 · 10 comments

Comments

@umbrella-ivan-zhukov
Copy link

In external request callback loopback.getCurrenrContext() returns 'null'.
Is this an expected behavior?

@BerkeleyTrue
Copy link
Contributor

Have you set the remoting context setting as here. If so, then this is likely a bug.

@umbrella-ivan-zhukov
Copy link
Author

Yes, I have this setting.
It seems, while the response has not been sent, external request rewrites the current context with the new one. And I really need the initial context throughout the request processing.

@BerkeleyTrue
Copy link
Contributor

Context is within a domain of each request so each request has its own context. Additional request should not overwrite the others.

If there is no request there is no context to create.

How exactly are you trying to use it. Maybe I am just misunderstanding your use case. Can you provide some example code?

@umbrella-ivan-zhukov
Copy link
Author

@BerkeleyTrue , thanks for your response! Here is the simplified code:

var loopback = require('loopback'),
    thirdPartyService = require('thirdPartyService');

module.exports = function(User) {

  User.cardDetails = function(callback) {
    // Fetch user data
    thirdPartyService.fetchUser({
      userId: loopback.getCurrentContext().get('user').id  /* works as expected */
    }, function(error, user) {
      if (error) throw error;

      // Fetch card details
      thirdPartyService.fetchCard({
        cardId: user.default_card,
        userId: loopback.getCurrentContext().get('user').id  /* throw error, because getCurrentContext returns null */
      }, function(error, card) {
        if (error) throw error;

        // Respond with card data
        callback(null, card);
      })
    })
  };

  User.remoteMethod('cardDetails', {
    http: { path: '/cardDetails', verb: 'get' }
  });
}

@BerkeleyTrue
Copy link
Contributor

Yes, that seems like a bug. The domain for the fetchUser callback should be the same as the domain for cardDetails callback.

It may also be that the callback for thirdPartyService is loosing the domain since it's outside of loopback? Not sure on that one. Will need a Strongloop Dev or someone more familiar with Domains to chime in here.

Here is a quick work around for you.

var loopback = require('loopback'),
    thirdPartyService = require('thirdPartyService');

module.exports = function(User) {

  User.cardDetails = function(callback) {
     var userId  = loopback.getCurrentContext().get('user').id;
     // Now userId is in the closure for this function.
    // Fetch user data
    thirdPartyService.fetchUser({
      userId: userId
    }, function(error, user) {
      if (error) throw error;

      // Fetch card details
      thirdPartyService.fetchCard({
        cardId: user.default_card,
        userId: userId
      }, function(error, card) {
        if (error) throw error;

        // Respond with card data
        callback(null, card);
      })
    })
  };

  User.remoteMethod('cardDetails', {
    http: { path: '/cardDetails', verb: 'get' }
  });
}

@umbrella-ivan-zhukov
Copy link
Author

Unfortunately it does not work for model hooks, that called after external request.

@umbrella-ivan-zhukov
Copy link
Author

Do anybody have suggestions why context gets null after external http request?
I'm stucked with this bug, also tried to use node.js domains directly, but failed.

@superkhau
Copy link
Contributor

Can you provide a link to a test project on Github? See https://github.com/strongloop/loopback/wiki/Issues#bug-report

@loay
Copy link
Contributor

loay commented Mar 23, 2016

getCurrentContext is deprecated: #1676 but I am not sure if it is related to your case, so if you are still running into the same issue, can you please provide a link to your test project to reproduce? Thanks.

@umbrella-ivan-zhukov
Copy link
Author

@loay , thanks for information.
Yes, seems like my issue:

It seems to me that most of the issues are caused by the fact that any connector using connection pool must be CLS aware and include few lines of code to switch to the right CLS context.

You can view my fix here, where I'm looking data through all available contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants