TypeError:无法在nodejs中读取未定义的属性“email”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError:无法在nodejs中读取未定义的属性“email”相关的知识,希望对你有一定的参考价值。
我正在尝试使用nodejs制作api。在这里我正在为api制作注册路线,但问题是我每次尝试注册用户时都说这个。这是我得到的整个错误。
TypeError:无法读取未定义的属性“email”
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:projectslog-mernackend
ode_modu
lesexpresslib
outerlayer.js:95:5)
at next (C:projectslog-mernackend
ode_modulesexpresslib
outer
oute
.js:137:13)
at Route.dispatch (C:projectslog-mernackend
ode_modulesexpresslib
o
uter
oute.js:112:3)
at Layer.handle [as handle_request] (C:projectslog-mernackend
ode_modu
lesexpresslib
outerlayer.js:95:5)
at C:projectslog-mernackend
ode_modulesexpresslib
outerindex.js:28
1:22
at Function.process_params (C:projectslog-mernackend
ode_modulesexpre
sslib
outerindex.js:335:12)
at next (C:projectslog-mernackend
ode_modulesexpresslib
outerindex
.js:275:10)
at Function.handle (C:projectslog-mernackend
ode_modulesexpresslib
outerindex.js:174:3)
at router (C:projectslog-mernackend
ode_modulesexpresslib
outerind
ex.js:47:12)
at Layer.handle [as handle_request] (C:projectslog-mernackend
ode_modu
lesexpresslib
outerlayer.js:95:5)
at trim_prefix (C:projectslog-mernackend
ode_modulesexpresslib
oute
rindex.js:317:13)
at C:projectslog-mernackend
ode_modulesexpresslib
outerindex.js:28
4:7
at Function.process_params (C:projectslog-mernackend
ode_modulesexpre
sslib
outerindex.js:335:12)
at next (C:projectslog-mernackend
ode_modulesexpresslib
outerindex
.js:275:10)
at C:projectslog-mernackend
ode_modulesody-parserlib
ead.js:130:5
这是我的代码
车型/ 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);
路线/ 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;
答案
您的代码的问题是您正在尝试从users
读取不存在的req.body
密钥。您的origin req对象不包含users
。
如果你console.log(req.body)
,req.body
看起来将完全像你的要求:
{
email: "adityakmr088@gmail.com"
passwordHash: "12345"
}
将您的代码更改为:
const { email, passwordHash } = req.body;
const user = new User({ email });
以上是关于TypeError:无法在nodejs中读取未定义的属性“email”的主要内容,如果未能解决你的问题,请参考以下文章
NodeJs,mongoDB:TypeError:无法读取未定义的属性“全名”
TypeError:无法读取未定义的属性“get”(nodejs)
TypeError:无法读取未定义的nodejs请求http的属性'0'
nodejs/mongoDB - 类型错误:无法读取未定义的属性“集合”