loopback-context icon indicating copy to clipboard operation
loopback-context copied to clipboard

Always got null context in myModel.js file

Open NaimishKher95 opened this issue 8 years ago • 5 comments

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

NaimishKher95 avatar May 08 '18 10:05 NaimishKher95

Me too, LoopBackContext.getCurrentContext().get() In node version 10.1.0 alway return null. Switch to version 10.5.0 return normal;

FengYuHe avatar Jul 10 '18 03:07 FengYuHe

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?

virkt25 avatar Jul 10 '18 04:07 virkt25

I am experiencing the same errors any update

charliekaks avatar May 30 '19 07:05 charliekaks

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.

stale[bot] avatar Feb 11 '20 03:02 stale[bot]

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.

stale[bot] avatar Feb 27 '20 05:02 stale[bot]