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

loopback.getCurrentContext() return null #2645

Closed
JoeShi opened this issue Aug 17, 2016 · 2 comments
Closed

loopback.getCurrentContext() return null #2645

JoeShi opened this issue Aug 17, 2016 · 2 comments
Labels

Comments

@JoeShi
Copy link

JoeShi commented Aug 17, 2016

I am just following the Using Current Context guide to get the current user. However, it always return null.

The following is my code, same as in the docs, the only modification is the print message.

app.use(function setCurrentUser(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.'));
    }
    var loopbackContext = loopback.getCurrentContext();


    console.log(loopbackContext);
    console.log('after loopbackContext');

    if (loopbackContext) {
      loopbackContext.set('currentUser', user);
    }
    next();
  });
});

my package.json

{
  "name": "caiyi",
  "version": "0.0.1",
  "main": "server/server.js",
  "scripts": {
    "start": "node .",
    "pretest": "jshint ."
  },
  "dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "express-flash": "0.0.2",
    "loopback": "^2.22.0",
    "loopback-boot": "^2.6.5",
    "loopback-component-explorer": "^2.1.0",
    "loopback-component-passport": "^2.1.0",
    "loopback-connector-mongodb": "^1.15.2",
    "loopback-datasource-juggler": "^2.39.0",
    "loopback-passport": "^1.0.2",
    "loopback-remote-routing": "^1.3.0",
    "moment": "^2.14.1",
    "qcloud_cos": "^1.0.4",
    "request": "^2.74.0",
    "serve-favicon": "^2.0.1",
    "wechat": "^2.0.3",
    "wechat-api": "^1.28.0",
    "wechat-oauth": "^1.1.1",
    "wechat-pay": "^0.2.4"
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "jshint": "^2.5.6",
    "grunt-assemble": "^0.4.0",
    "grunt-bump": "0.0.15",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-concat": "^0.5.0",
    "grunt-contrib-copy": "^0.5.0",
    "grunt-contrib-cssmin": "^0.14.0",
    "grunt-contrib-htmlmin": "^0.3.0",
    "grunt-contrib-sass": "^0.9.2",
    "grunt-contrib-uglify": "^0.5.1",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-sass": "^1.0.0",
    "grunt-usemin": "^3.1.1"
  },
  "repository": {
    "type": "",
    "url": ""
  },
  "description": "caiyi"
}
@alexkolson
Copy link

@JoeShi See #2251. In short, getCurrentContext() is not reliable and should be avoided.

@0candy
Copy link
Contributor

0candy commented Aug 17, 2016

@alexkolson Thank you for responding.
@JoeShi Closing this as a dup.

@0candy 0candy closed this as completed Aug 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants