通过手柄部分传递用户对象时注销错误

问题描述

因此,我试图在导航栏上呈现用户名称。到目前为止,我有一个routes文件夹,views文件夹和一个局部文件夹。当我尝试在个人资料页面和主页上呈现用户名时,它就起作用了。

就这样:

router.get('/',(req,res) => {
  res.render('index',{
    title: 'Site',name: req.user.username,// <-- Line 10
    image: req.user.image,});
});

// @desc     Profile
// @route    GET/profile
router.get('/profile',res) => {
  res.render('profile',{
    name: req.user.username,title: req.user.username,image: req.user.image,});
});

但是,当我进入/ logout时,我得到了:

TypeError: Cannot read property 'username' of undefined
    at C:\Users\Desktop\Code\site\routes\index.js:10:20 

如果我删除第10行和第11行,然后尝试注销,则可以正常运行。我虽然这可能是一个问题,因为_nav是局部的,但是为什么我仅在'/'上使用它时才会出现错误

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)