为什么我的节点验证在此 POST 中失败?
Why is my Node validation failing in this POST?
我正在学习 MEAN machine 节点身份验证教程。
这是他们的源代码:https://github.com/scotch-io/mean-machine-code/blob/master/10-node-authentication/server.js除了apiRouter.post('/authenticate',
部分
,我基本上什么都有
Express API 正在运行:
http://localhost:8615/api/users
将 return 来自 scotch.io 的 MongoDB
的用户列表
以下是/api/users的API:
apiRouter.route('/users')
// create a user (accessed at POST http://localhost:8615/api/users)
.post(function(req, res) {
// create a new instance of the User model
var user = new User();
// set the users information (comes from the request)
user.name = req.body.name;
user.username = req.body.username;
user.password = req.body.password;
// save the user and check for errors
user.save(function(err) {
if (err) {
// duplicate entry
if (err.code == 11000)
return res.json({ success: false, message: 'A user with that username already exists. '});
else
return res.send(err);
}
// return a message
res.json({ message: 'User created!' });
});
})
// get all users (access at GET http://localhost:8615/api/users)
.get(function(req, res) {
User.find(function(err, users) {
if (err) return res.send(err);
// return the users
res.json(users);
})
});
这是我的 user.js 用户架构
// SCHEMAS ------------------------------------
// user schema
var UserSchema = new Schema({
name: String,
username: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true, select: false }
// ^ select false will not return passwords
});
// hash the password before the user is saved
UserSchema.pre('save', function(next) {
var user = this;
// PUT username
if (!user.isModified('username')) return next();
// PUT name
if (!user.isModified('name')) return next();
// hash the password only if the password has been changed or user is new
if (!user.isModifited('password')) return next();
// generate the salt
bcrypt.hash(user.password, null, null, function(err, hash) {
if (err) return next(err);
// change the password to the hashed version
user.password = hash;
next();
});
});
摘自本书:
Create a Sample User
First, we need to make sure that we even have a user to authenticate
since towards the end of last chapter, we deleted everyone. Let’s
create the user using the POST http://localhost:8080/api/users route
we created in our API to add a user to our database.
We will send a POST request with the following information: Name Chris
Username chris Password supersecret
我正在使用 Postman 添加新用户,如您所见,我已经输入了 key/value 对用户名和密码,但是收到一条错误消息 "Validation failed" "username is required" "password is required":
更新,我刚刚尝试了 x-www-form-urlencoded 并收到以下错误
GET /api/users 200 66.141 ms - 655
••• API CRUD hit •••
/Users/leongaban/NodeDallas/projects/awesome-test/app/models/user.js:27
if (!user.isModifited('password')) return next();
^
TypeError: Object { password: 'supersecret',
username: 'Chris',
name: 'chris',
_id: 54c001dc4ee4028c18e61334 } has no method 'isModifited'
at model.UserSchema.methods.comparePassword.user (/Users/leongaban/NodeDallas/projects/awesome-test/app/models/user.js:27:12)
Postman 错误截图:https://s3.amazonaws.com/f.cl.ly/items/1M0M392M0E3b2i430I1n/Image%202015-01-21%20at%201.45.51%20PM.png
您想将 json 推送到您的服务器。 Select raw 并将数据类型设置为 JSON.
那么您只需在此处以 JSON 格式填写您的用户及其所有字段。
{
"name": "Chris",
"username": "chris",
"password": "supersecret"
}
在邮递员中尝试 x-www-form-urlencoded,就可以了。
我正在学习 MEAN machine 节点身份验证教程。
这是他们的源代码:https://github.com/scotch-io/mean-machine-code/blob/master/10-node-authentication/server.js除了apiRouter.post('/authenticate',
部分
Express API 正在运行:
http://localhost:8615/api/users
将 return 来自 scotch.io 的 MongoDB
以下是/api/users的API:
apiRouter.route('/users')
// create a user (accessed at POST http://localhost:8615/api/users)
.post(function(req, res) {
// create a new instance of the User model
var user = new User();
// set the users information (comes from the request)
user.name = req.body.name;
user.username = req.body.username;
user.password = req.body.password;
// save the user and check for errors
user.save(function(err) {
if (err) {
// duplicate entry
if (err.code == 11000)
return res.json({ success: false, message: 'A user with that username already exists. '});
else
return res.send(err);
}
// return a message
res.json({ message: 'User created!' });
});
})
// get all users (access at GET http://localhost:8615/api/users)
.get(function(req, res) {
User.find(function(err, users) {
if (err) return res.send(err);
// return the users
res.json(users);
})
});
这是我的 user.js 用户架构
// SCHEMAS ------------------------------------
// user schema
var UserSchema = new Schema({
name: String,
username: { type: String, required: true, index: { unique: true }},
password: { type: String, required: true, select: false }
// ^ select false will not return passwords
});
// hash the password before the user is saved
UserSchema.pre('save', function(next) {
var user = this;
// PUT username
if (!user.isModified('username')) return next();
// PUT name
if (!user.isModified('name')) return next();
// hash the password only if the password has been changed or user is new
if (!user.isModifited('password')) return next();
// generate the salt
bcrypt.hash(user.password, null, null, function(err, hash) {
if (err) return next(err);
// change the password to the hashed version
user.password = hash;
next();
});
});
摘自本书:
Create a Sample User
First, we need to make sure that we even have a user to authenticate since towards the end of last chapter, we deleted everyone. Let’s create the user using the POST http://localhost:8080/api/users route we created in our API to add a user to our database.
We will send a POST request with the following information: Name Chris Username chris Password supersecret
我正在使用 Postman 添加新用户,如您所见,我已经输入了 key/value 对用户名和密码,但是收到一条错误消息 "Validation failed" "username is required" "password is required":
更新,我刚刚尝试了 x-www-form-urlencoded 并收到以下错误
GET /api/users 200 66.141 ms - 655
••• API CRUD hit •••
/Users/leongaban/NodeDallas/projects/awesome-test/app/models/user.js:27
if (!user.isModifited('password')) return next();
^
TypeError: Object { password: 'supersecret',
username: 'Chris',
name: 'chris',
_id: 54c001dc4ee4028c18e61334 } has no method 'isModifited'
at model.UserSchema.methods.comparePassword.user (/Users/leongaban/NodeDallas/projects/awesome-test/app/models/user.js:27:12)
Postman 错误截图:https://s3.amazonaws.com/f.cl.ly/items/1M0M392M0E3b2i430I1n/Image%202015-01-21%20at%201.45.51%20PM.png
您想将 json 推送到您的服务器。 Select raw 并将数据类型设置为 JSON.
那么您只需在此处以 JSON 格式填写您的用户及其所有字段。
{
"name": "Chris",
"username": "chris",
"password": "supersecret"
}
在邮递员中尝试 x-www-form-urlencoded,就可以了。