我在向数据库添加team
时遇到问题,我现在有一个索引页面,目前只显示数据库中的所有团队。当我转到下面localhost:3000/team
的{{1}}时,我输入了团队名称,然后按提交按钮。当我进入索引页面时,我的新团队就在那里。
但是当我按下提交按钮时,我实际上收到一条错误消息:
team.jade
我不确定为什么会这样,Express
500 TypeError: Cannot read property 'teamName' of undefined
at module.exports (/home/declan/nodeapps/tournamentManager/routes/index.js:126:24)
at callbacks (/home/declan/nodeapps/tournamentManager/node_modules/express/lib/router/index.js:160:37)
at param (/home/declan/nodeapps/tournamentManager/node_modules/express/lib/router/index.js:134:11)
at pass (/home/declan/nodeapps/tournamentManager/node_modules/express/lib/router/index.js:141:5)
at Router._dispatch (/home/declan/nodeapps/tournamentManager/node_modules/express/lib/router/index.js:169:5)
at Object.router (/home/declan/nodeapps/tournamentManager/node_modules/express/lib/router/index.js:32:10)
at next (/home/declan/nodeapps/tournamentManager/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.handle (/home/declan/nodeapps/tournamentManager/app.js:34:5)
at next (/home/declan/nodeapps/tournamentManager/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.static (/home/declan/nodeapps/tournamentManager/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
的第126行实际上就是下面的这一行:
index.js
这是创建新数据库条目的第一行,因此如果它无法读取该属性,则不会将其添加到数据库中。这是我的想法,但当我重新加载索引页面时,有我的新数据库条目,那你为什么认为它不起作用?
index.js @ routes
var name = teamForm.teamName;
team.jade @ views(html模板,你可以看到所需的id等)
/**
* Add a new Team to database
*/
app.post('/team', function(req, res) {
util.log('Serving request for url[GET] ' + req.route.path);
var teamForm = req.body.teamForm;
var name = teamForm.teamName;
var newTeam = new Team();
newTeam.name = name;
newTeam.save(function(err, savedTeam){
var message = '';
var retStatus = '';
if(!err){
util.log('Successfully created team with Name : ' + name);
message = 'Successfully created new team : ' + name;
retStatus = 'success';
} else {
util.log('Error while creating team : ' + name + ' error : ' + util.inspect(err));
if(err.code === 11000){
message = 'Team already exists';
}
retStatus = 'failure';
}
res.json({
'retStatus' : retStatus,
'message' : message
});
});
});
team.js @ js
extends index
block content
div.row-fluid
div.span9
h2 New Team
div.well.sidebar-nav
div.teamList
form.form-horizontal(method="post", id="team-form")
div.control-group
label.control-label(for="teamName") Team Name :
div.controls
input.input-small(type="text", id="teamName")
div.control-group
div.controls
button#teamConfirm.btn.btn-primary.btn-mini(href='#') To DB
br
p This page is used to demonstrate how an 8 team single elimination tournament would be represented in the final design of my project. The pseudo code conjured up in my initial hand-in document was originally used here to produce the same result. The pseudo code helped create this set of seeded brackets, so that the matches correspond to the seeds of the tournament. For the 8 team bracket, I worked back through from the final, where seeds 1 and 2 should face each other, until I was left at round 1 of the brackets. This was used to give myself a 'new' set of seeds which were in the correct order to just be placed into these brackets.
div.span3
div.well.sidebar-nav
h4 To-Do List:
p - Enter list of teams manually from this page.
p - Do a test to show a bracket with random seed.
p - Show rankings after tournament
index.js @ js
var newTeam = function(){
$('#teamConfirm').click(function(){
newTeam.teamForm();
});
};
newTeam.teamForm = function(){
var teamForm = {
teamName : $('#teamName').val()
};
// Basic validation
$.post('/team', {'teamForm' : teamForm}, function(response) {
console.log(response);
});
};
newTeam();
答案 0 :(得分:0)
我认为这是因为req.body
没有被解析为JavaScript对象(它仍然是JSON字符串)。首先检查您是否使用
app.use(express.bodyParser());
在任何路线之前 。如果不是这样,请尝试使用.ajax
代替.post
设置contentType
:
$.ajax({
// some other settings
contentType : "application/json"
});
可能是内容类型混乱而Express未解析从客户端收到的JSON字符串。您可能尝试的另一件事就是(在服务器端):
app.post('/team', function(req, res) {
var body = JSON.parse( req.body );
// other code
}
也许用try{}catch{}
阻止包裹附加行。