我是Node的新手,我一直在尝试创建一个API并在邮递员中对其进行测试,但是从邮递员发送值后,我收到了此错误
password:
{ ValidatorError: Path `password` is required.
at new ValidatorError (D:\devconnector\node_modules\mongoose\lib\error\validator.js:29:11)
at validate (D:\devconnector\node_modules\mongoose\lib\schematype.js:978:13)
at D:\devconnector\node_modules\mongoose\lib\schematype.js:1031:11
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\devconnector\node_modules\mongoose\lib\schematype.js:987:14)
at D:\devconnector\node_modules\mongoose\lib\document.js:2101:9
at processTicksAndRejections (internal/process/next_tick.js:74:9)
message: 'Path `password` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'password',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true } },
_message: 'users validation failed',
name: 'ValidationError' }
下面是我的server.js脚本
const express = require('express');
const mongoose = require('mongoose');
const users = require('./routes/api/users');
const profile = require('./routes/api/profile');
const posts = require('./routes/api/posts');
const bodyParser = require('body-parser');
const app = express();
//Body Parser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//DB Config
const db = require('./config/keys').mongoURI;
//Connect to MongoDB
mongoose.connect(db).then(()=> console.log('MongoDB Connected')).catch(err => console.log(err));
app.get('/',(req,res)=>res.send('Hello World'));
//use routes
app.use('/api/users',users);
app.use('/api/profile',profile);
app.use('/api/posts',posts);
const port = process.env.PORT || 5000;
app.listen(port, ()=> console.log(`Server running on port ${port}`));
这是我的key.js脚本
module.exports={
//mongoURI:'mongodb://root:qwerty123@devconnector-u2cub.mongodb.net:27017'
mongoURI:'mongodb://127.0.0.1:27017/devconnector'
};
users.js脚本
const express = require('express');
const router = express.Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
// Load User model
const User = require('../../models/User');
//@route GET api/users/test
//@desc Tests post route
//@ access public
router.get('/test',(req, res) => res.json({msg: "Users Works2"}));
//@route GET api/users/register
//@desc Register user
//@ access public
router.post('/register',(req, res)=>{
User.findOne({email:req.body.email})
.then(user=>{
if(user){
return res.status(400).json({email: 'Email already exists'});
}else{
const avatar = gravatar.url(req.body.email,{
s:'200',//size
r: 'pg',//rating
d: 'mm' //Default
})
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt)=>{
bcrypt.hash(newUser.password, salt, (err, hash)=>{
//if(err)throw err;
newUser.password = hash;
newUser.save()
.then(user=> res.json(user))
.catch(err=> console.log(err));
})
})
}
})
});
module.exports = router;
最后我的用户模型
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Create Schema
const UserSchema = new Schema({
name:{
type: String,
required: true,
},
email:{
type: String,
required: true
},
password:{
type: String,
required: true
},
avatar:{
type: String,
},
date:{
type: Date,
default: Date.now
},
});
module.exports = User = mongoose.model('users', UserSchema);
这也是邮递员的错误消息
Could not get any response
There was an error connecting to http://localhost:5000/api/users/register?name=RUGUMBIRA&email=rugumbirajordybastien@gmail.com&password=123456.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General
请帮助,我在做什么错
答案 0 :(得分:0)
从服务器上的错误消息中,您似乎正在使用Validator模块作为验证中间件,并且必须输入密码字段。但是,当您尝试在req.body对象中尝试访问注册详细信息时,它似乎也会作为查询传递。如果是这种情况,Validator会将req.body视为空,然后抛出错误。
与其在查询中传递名称,电子邮件和密码,不如将它们写入Postman请求的正文中。单击Postman应用程序上的“正文”选项卡,然后输入注册详细信息。