Javascript MongoDB/Node.js-网站崩溃但数据库更新
我以前写过一篇文章,有点类似,但我还没有修复它 首先,这是我的终端:Javascript MongoDB/Node.js-网站崩溃但数据库更新,javascript,json,node.js,mongodb,Javascript,Json,Node.js,Mongodb,我以前写过一篇文章,有点类似,但我还没有修复它 首先,这是我的终端: 23 Dec 22:31:23 - Serving request for url[GET] /team 23 Dec 22:31:23 - Successfully created team with Name : Japan 23 Dec 22:31:23 - Serving request for url[GET] /team TypeError: Cannot read property 'teamName' of u
23 Dec 22:31:23 - Serving request for url[GET] /team
23 Dec 22:31:23 - Successfully created team with Name : Japan
23 Dec 22:31:23 - Serving request for url[GET] /team
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)
因此,它成功地在数据库中创建了一个良好的条目,但在此之后,当它尝试重定向回“/team”时,页面崩溃,该页面位于以下位置:
index.js
路由文件
/**
* 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
});
});
});
从那以后,我取得了一些进步,我在app.js
文件中添加了bodyParser()
,这是我以前没有做过的,但是我被告知它崩溃了,因为req.body
没有被解析或者它被当作错误的格式
这也是team.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();
有没有办法解决这个问题?问题出在您的
$上。在调用后,它应该使用contentType=application/json
头发送数据。别问为什么
所以如果你把它改成这样:
$.ajax({
url:'/team',
type:"POST",
data: JSON.stringify({teamFrom:teamFrom}),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(response){
console.log(response);
}
})
在节点端,通过console.log('body:',req.body),调试chekc在req.body
中的内容代码>
更多阅读
及
,特别是来自SooDesuNe的一条评论,你说它在重定向时崩溃,在重定向发生的地方发布代码我相信这更多地与index.js
文件中的teamForm
有关。当我打印到控制台时,它接收到的数据是未定义的,我不太理解,因为它仍然创建了一个新的数据库入口。嗨,有趣的帖子,我尝试过使用您编写的ajax调用帖子,但仍然不起作用。新团队已成功添加到数据库中,但控制台中将req.body
打印为undefined
。我仍然不知道为什么,当然它应该与您的ajax代码一起工作更准确地说,它打印body:{}
对不起,但是req.body.teamForm.name
是否打印团队名称问题只是在重新加载页面时出现的,我刚刚注意到这一点