无法读取Cookie的属性验证

时间:2019-01-27 08:58:03

标签: node.js express cookies

我正在设置一个cookie,当我从浏览器中查看该cookie时,该cookie已成功保存,但是,当我尝试从另一个文件中读取它的值时,出现错误“ TypeError:无法读取未定义的属性'auth' “ ,其中auth是cookie的名称。

const User=require('../models/user');

const authenticate = (req, res, next) => {
let authTken = req.cookie.auth;
console.log(authTken);
return User.findbyTokenAndValidate(authTken)
.then((user)=>{
    if (user)
    return next();
    else return Promise.reject()
})
.catch((err)=>{

    console.log( err);
res.status('401');                                                                                                                                                    

})

这是我遇到的错误: TypeError:无法读取未定义的属性“ auth”     在验证时(/Users/Mohannad/Desktop/WORK/Book/middleware/authenticate.js:4:31)     在Layer.handle [作为handle_request](/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/layer.js:95:5)     在下一个(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/route.js:137:13)     在Route.dispatch(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/route.js:112:3)     在Layer.handle [作为handle_request](/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/layer.js:95:5)     在/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:281:22     在Function.process_params(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:335:12)     在下一个(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:275:10)     在Function.handle(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:174:3)     在路由器上(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:47:12)     在Layer.handle [作为handle_request](/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/layer.js:95:5)     在trim_prefix(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:317:13)     在/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:284:7     在Function.process_params(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:335:12)     在下一个(/Users/Mohannad/Desktop/WORK/Book/node_modules/express/lib/router/index.js:275:10)     在SendStream.error(/Users/Mohannad/Desktop/WORK/Book/node_modules/serve-static/index.js:121:7)

1 个答案:

答案 0 :(得分:1)

您要使用的是req.cookies,而不是req.cookie