【问题标题】:Sails.js can't access data model in the middleware: Unexpected tokenSails.js 无法访问中间件中的数据模型:Unexpected token
【发布时间】:2018-04-06 13:50:50
【问题描述】:

sails.js 新手在这里。

我无法在我的中间件中访问我的 User 模型。它说的是意外令牌。

这是我的中间件,

        isAuthenticated: (function(){   
            return function authHandler(req, res, next) {

                let payload;   
                try {          
                    payload = decode(req);          
                    let expTime = moment.tz(payload.exp, 'GMT').toDate();
                    let currentTIme = moment.tz('GMT').toDate();

                    if (currentTIme > expTime) {    
                        return res.status(401).json({message: 'JWT token expired.'}); 
                    } else {   
>>                      const user =  await User.findOne({id: payload.id}); 

                        if (user) {                     
                            req.payload = {                 
                                userId: user.id                 
                            }; 
                            return next()                   

                        } else {
                            return res.status(401).json({message: 'User doesn\'t exist.'});

                        }      
                    }          

                } catch (err) {
                    return res.serverError();       
                }              


            }                  
        })()                   
    }

我正在尝试设置身份验证中间件。在我的全局设置中,models 设置为 true

我试过了,sails.models.user,但即便如此,我还是得到了意外的令牌。

【问题讨论】:

    标签: sails.js


    【解决方案1】:

    您需要输入async 关键字async function(..){....await....}await 关键字仅在异步函数内部有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-27
      • 2018-05-10
      • 2014-03-30
      • 2023-04-02
      • 2014-04-27
      • 2014-01-26
      • 1970-01-01
      • 2015-03-20
      相关资源
      最近更新 更多