Body-Parser 给出未定义的
Body-Parser gives undefined
我的 server.js 使用以下代码对用户进行身份验证:
//API ROUTES
var apiRoutes = express.Router();
//route to auth user
apiRoutes.post('/authenticate', function(req,res){
//find the user
User.findOne({
name: req.body.name
}, function(err,user){
if(err) throw err;
if(!user){
res.json({ success: false, message: 'Authentication failed. User not found!' });
} else if(user){
//check if password matches
if(user.password != req.body.password){
res.json({ success: false, message: 'Authentication failed. Wrong password!' });
} else{
//user found and password is right
//toke creation
var token = jwt.sign(user, app.get('superSecret'),{
expiresInMinutes: 1440 // = 24h
});
//return information including token
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
}
}
});
});
但即使插入正确的 Id,输出也始终是 "Authentication failed. User not found!"
我使用以下代码在 MongoDb 中插入用户:
app.get('/setup',function(req,res){
var nick = new User({
name: 'patro',
password: 'pass',
admin: true
});
nick.save(function(err){
if(err)throw err;
console.log('user saved');
res.json({success : true});
});
});
并且用户被正确保存,因为使用以下代码:
apiRoutes.get('/users',function(req,res){
User.find({}, function(err,users){
res.json(users);
});
});
通过使用 POSTman,我可以看到用户已正确保存:
{
"_id": "5884844c338f4813ab884eac",
"name": "patro",
"password": "pass",
"admin": true,
"__v": 0
}
名称永远不会匹配,因为 req.body.name
returns undefined
作为值。
为什么?我应该如何发送名称的值?
正文解析器的使用如下所示:
...
var app = express();
var bodyParser = require('body-parser');
...
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//没有参数请不要评论,这将是下一步:)
以下是postman app的截图,请问您应该如何post请求。如有不明之处请评论
我的 server.js 使用以下代码对用户进行身份验证:
//API ROUTES
var apiRoutes = express.Router();
//route to auth user
apiRoutes.post('/authenticate', function(req,res){
//find the user
User.findOne({
name: req.body.name
}, function(err,user){
if(err) throw err;
if(!user){
res.json({ success: false, message: 'Authentication failed. User not found!' });
} else if(user){
//check if password matches
if(user.password != req.body.password){
res.json({ success: false, message: 'Authentication failed. Wrong password!' });
} else{
//user found and password is right
//toke creation
var token = jwt.sign(user, app.get('superSecret'),{
expiresInMinutes: 1440 // = 24h
});
//return information including token
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
}
}
});
});
但即使插入正确的 Id,输出也始终是 "Authentication failed. User not found!"
我使用以下代码在 MongoDb 中插入用户:
app.get('/setup',function(req,res){
var nick = new User({
name: 'patro',
password: 'pass',
admin: true
});
nick.save(function(err){
if(err)throw err;
console.log('user saved');
res.json({success : true});
});
});
并且用户被正确保存,因为使用以下代码:
apiRoutes.get('/users',function(req,res){
User.find({}, function(err,users){
res.json(users);
});
});
通过使用 POSTman,我可以看到用户已正确保存:
{
"_id": "5884844c338f4813ab884eac",
"name": "patro",
"password": "pass",
"admin": true,
"__v": 0
}
名称永远不会匹配,因为 req.body.name
returns undefined
作为值。
为什么?我应该如何发送名称的值?
正文解析器的使用如下所示:
...
var app = express();
var bodyParser = require('body-parser');
...
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//没有参数请不要评论,这将是下一步:)
以下是postman app的截图,请问您应该如何post请求。如有不明之处请评论