【发布时间】:2020-09-03 05:10:27
【问题描述】:
我目前正在开发一个博客应用程序。我使用 react js 作为前端,使用 node js 作为后端。我有这个 isAuthenticated 方法。
export const isAuthenticated = () => {
if (typeof window == 'undefined') {
return false;
}
if (localStorage.getItem('jwt')) {
return JSON.parse(localStorage.getItem('jwt'));
}
else {
return false;
}};
我在这里使用它
<li className="nav-item">
<Link
to={`/user/${isAuthenticated().user._id}`}
style={isActive(history, `/user/${isAuthenticated().user._id}`)}
className="nav-link"
>
{`${isAuthenticated().user.name}'s profile`}
</Link>
</li>
它给了我 TypeError 的错误:无法读取未定义的属性 '_id'
to={/user/${isAuthenticated().user._id}}这部分
请帮帮我,我想我无法从后端获取 _id,但每当我在 google chrome 开发工具中单击应用程序时,我都可以使用 jwt 令牌
【问题讨论】:
标签: javascript node.js reactjs web