TypeError:无法读取nodejs中未定义的属性“ email”

时间:2019-04-14 08:37:37

标签: javascript node.js mongoose mongoose-schema

我正在尝试使用nodejs制作api。在这里,我正在为api进行注册,但是问题是每次我尝试注册用户时都说这。 这是我得到的全部错误。

  

TypeError:无法读取未定义的属性“电子邮件”

TypeError: Cannot read property 'email' of undefined
    at C:/projects/blog-mern/backend/src/routes/users.js:7:13
    at Layer.handle [as handle_request] (C:\projects\blog-mern\backend\node_modu
les\express\lib\router\layer.js:95:5)
    at next (C:\projects\blog-mern\backend\node_modules\express\lib\router\route
.js:137:13)
    at Route.dispatch (C:\projects\blog-mern\backend\node_modules\express\lib\ro
uter\route.js:112:3)
    at Layer.handle [as handle_request] (C:\projects\blog-mern\backend\node_modu
les\express\lib\router\layer.js:95:5)
    at C:\projects\blog-mern\backend\node_modules\express\lib\router\index.js:28
1:22
    at Function.process_params (C:\projects\blog-mern\backend\node_modules\expre
ss\lib\router\index.js:335:12)
    at next (C:\projects\blog-mern\backend\node_modules\express\lib\router\index
.js:275:10)
    at Function.handle (C:\projects\blog-mern\backend\node_modules\express\lib\r
outer\index.js:174:3)
    at router (C:\projects\blog-mern\backend\node_modules\express\lib\router\ind
ex.js:47:12)
    at Layer.handle [as handle_request] (C:\projects\blog-mern\backend\node_modu
les\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\projects\blog-mern\backend\node_modules\express\lib\route
r\index.js:317:13)
    at C:\projects\blog-mern\backend\node_modules\express\lib\router\index.js:28
4:7
    at Function.process_params (C:\projects\blog-mern\backend\node_modules\expre
ss\lib\router\index.js:335:12)
    at next (C:\projects\blog-mern\backend\node_modules\express\lib\router\index
.js:275:10)
    at C:\projects\blog-mern\backend\node_modules\body-parser\lib\read.js:130:5

这是我的代码

models / users.js

import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

const schema = new mongoose.Schema({
    email: {
        type: String,
        required: true
    },
    passwordHash: {
        type: String,
        required: true
    }
}, {
    timestamps: true
});

schema.methods.setPassword = function setPassword(password) {
    this.passwordHash = bcrypt.hashSync(password, 10)
}

schema.methods.generateJWT = function generateJWT() {
    return jwt.sign({
        email: this.email,
    }, 'secret')
}

schema.methods.toAuthJSON = function toAuthJSON() {
    return {
        email: this.email,
        token: this.generateJWT()
    }
}

export default mongoose.model('User', schema);

routes / users.js

import express from 'express';
import User from '../models/User';

const router = express.Router();

router.post('/', (req, res, next) => {
    const { email, password } = req.body.users;
    const user = new User({ email });
    user.setPassword(password);
    user.save().then(userRecord => {
        res.json({user: userRecord.toAuthJSON()})
    }).catch(err => {
        res.status(500).json({
            error: err
        })
    })
})

export default router;
  

我正在使用邮递员发送请求   parmas detail

1 个答案:

答案 0 :(得分:2)

您的代码存在问题,您正在尝试从users中读取req.body键,该键不存在。您的原始请求对象不包含users

如果您console.log(req.body),则req.body看起来将与您的请求完全相同:

{
  email: "adityakmr088@gmail.com"
  passwordHash: "12345"
}

将代码更改为:

const { email, passwordHash } = req.body;
const user = new User({ email });