Loopbackjs 如何在环回js中设置中间件?

Loopbackjs 如何在环回js中设置中间件?,loopbackjs,Loopbackjs,我需要在身份验证/登录之后设置会话中的数据,所以我设置了如下中间件 middleware.json { "initial": { }, "session": { "express-session": { "params": { "secret": "mysceret", "saveUninitialized": true, "resave": true

我需要在身份验证/登录之后设置会话中的数据,所以我设置了如下中间件

middleware.json

{  
      "initial": { },
      "session": {
        "express-session": {
          "params": {
          "secret": "mysceret",
          "saveUninitialized": true,
          "resave": true
          }
        }
      },
      "auth:before": {},
      "auth": {
        "loopback#token": {}
      },
      "auth:after": { 
        "./middleware/store-current-user": {}
      },
      "parse": { }
    }
在my store-current-user.js中:

module.exports = function (options) {
       console.log(" it is working here");
    return function storeCurrentUser(req, res, next) {
         console.log(" it is not working here");
        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.'));
                }else{
                    console.log(' ok '); // it is not working.
                    req.session.user = user;
                    next();
                }
            });        
    };
};
快速会话:“快速会话”:“^1.15.6”。 环回版本:“环回”:“^3.0.0”

我在哪里失踪?我想不出来


请帮忙。

试试这样的东西好吗

module.exports = function () {
       console.log(" it is working here");
    return function storeCurrentUser(req, res, next) {
         console.log(" it is not working here");
        if (!req.accessToken) {
            next();
        }
       app.models.User.findById(req.accessToken.userId, function (err, user) {
                if (err) {
                   next(err);
                }
                if (!user) {
                    next(new Error('No user with this access token was found.'));
                }else{
                    console.log(' ok '); // it is not working.
                    req.session.user = user;
                    next();
                }
            });        
    };
};
  • 删除
    选项
    (如果需要,请稍后再添加)
  • 不要调用
    returnnext()
    ,只需单独调用
    next()
  • 另外,您是否在某处定义应用程序

该功能是否运行?您要传递的
选项是什么?@JackVaughan,是的,它正在工作,而“选项”当前未被使用。“应用程序”定义为单独的,我注意到它在切换我的应用程序中的其他路由(如“用户配置文件”)时起作用,因此环回阶段有一些问题,或者我错过了一些步骤。因为它不是在“auth:after middleware”之后执行的。不会抛出错误吗?当我在这样的函数中引用app时,我将app中的内容传递给该函数,以便它了解它。