首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JWT和.identify在反应中的问题

JWT和.identify在反应中的问题
EN

Stack Overflow用户
提问于 2021-04-06 11:31:58
回答 4查看 37关注 0票数 1

我已经分叉了:当我尝试登录到用户帐户时,https://github.com/ammarnadeeem/diaryExpress-react-flask-mysql和我在reactjs和获取配置文件信息方面遇到了一些问题。在我的MySQL中,用户、密码、电子邮件等都已注册,但似乎存在一个带有令牌的错误。这是我的代码:

代码语言:javascript
复制
  componentDidMount() {
    const token = localStorage.usertoken;
    const { identity } = jwt_decode(token);
    this.setState({
      first_name: identity.first_name,
      last_name: identity.last_name,
      email: identity.email,
    });
  }

她的错误是:

代码语言:javascript
复制
> TypeError: Cannot read property 'first_name' of undefined
> Profile.componentDidMount src/components/Profile.js:22   19 | const
> token = localStorage.usertoken;   20 | const { identity } =
> jwt_decode(token);   21 | this.setState({
> > 22 |   first_name: identity.first_name,   23 |   last_name: identity.last_name,   24 |   email: identity.email,   25 | }); View
> compiled ▶ 17 stack frames were collapsed. ./src/index.js
> src/index.js:7    4 | import App from './App';    5 | import
> registerServiceWorker from './registerServiceWorker';    6 | 
> >  7 | ReactDOM.render(<App />, document.getElementById('root'));    8 | registerServiceWorker();    9 |    10 |  View compiled ▼ 6 stack
> frames were expanded.
> __webpack_require__ /home/basileus/diaryExpress-react-flask-mysql/webpack/bootstrap
> 2773a34b4f4e3e36f9f0:678 fn
> /home/basileus/diaryExpress-react-flask-mysql/webpack/bootstrap
> 2773a34b4f4e3e36f9f0:88 0
> http://193.69.47.200:3000/static/js/bundle.js:76680:18
> __webpack_require__ /home/basileus/diaryExpress-react-flask-mysql/webpack/bootstrap
> 2773a34b4f4e3e36f9f0:678 (anonymous function)
> /home/basileus/diaryExpress-react-flask-mysql/webpack/bootstrap
> 2773a34b4f4e3e36f9f0:724 (anonymous function)
> http://193.69.47.200:3000/static/js/bundle.js:728:10 ▲ 6 stack frames
> were expanded. This screen is visible only in development. It will not
> appear if the app crashes in producti
EN

回答 4

Stack Overflow用户

发布于 2021-04-06 20:36:24

某种具有{标识}的东西-那么您需要用另一种方式对它进行排序吗?

票数 0
EN

Stack Overflow用户

发布于 2021-04-07 12:19:10

现在它是{ identity }-但是为什么使用{}。把他们带走,我想会管用的。

票数 0
EN

Stack Overflow用户

发布于 2021-04-07 12:23:04

根据jwt-解码的说法。

代码语言:javascript
复制
var decoded = jwt_decode(token);
 
console.log(decoded);
 
/* prints:
 * { foo: "bar",
 *   exp: 1393286893,
 *   iat: 1393268893  }
 */

因此,您应该使用

代码语言:javascript
复制
const identity = jwt_decode(token);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66967825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档