search

Home  >  Q&A  >  body text

node.js 报错 Cannot read property 'username' of undefined

点击登录就提示这个错误,但找不到原因

 TypeError: **Cannot read property 'username' of undefined**

login.js

前台js:获取输入的username password 拼成一个数组传给后台,

怪我咯怪我咯2875 days ago1230

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 14:45:25

    First: You should make sure ajax method is same as routers' method
    Second: You should make sure parse the data in body.

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
      extended: true
    }));

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:45:25

    contentType: "application/json;charset=utf-8"

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:45:25

    bodyThe data cannot be obtained.
    It may be that header is application/x-www-form-urlencoded, and datatype is caused by JSON.

    If you want to obtain JSON data in the background, you need to ensure that the middleware of express is used

    app.use(bodyParser.json());

    reply
    0
  • 阿神

    阿神2017-04-17 14:45:25

    How did the original poster solve it? I encountered the same problem

    TypeError: Cannot read property 'name' of undefined

    reply
    0
  • Cancelreply