MongoDB / Node.js -网站崩溃,但数据库更新
MongoDB / Node.js - Website crashes but database updated
我以前做过一个类似的帖子,但我还没有修复它。
首先,这是我的终端: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
});
});
});
我从以前取得了一些进展,我已经将bodyParser()
添加到app.js
文件中,这是我以前没有做过的,但我被告知它正在崩溃,因为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();
有什么办法解决这个问题吗?
问题是您的$.post
调用,它应该发送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);
}
})
在节点端,用于调试检查req.body
中的内容,通过console.log( 'body: ', req.body );
更多的阅读Jquery -如何使$.post()使用contentType=application/json?
和
如何检索POST查询参数?,特别是来自SooDesuNe的评论
相关文章:
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- 用sql数据库更新谷歌地图标记
- 数据库更新后刷新数据表
- 通知浏览器数据库更新的最佳方法
- AJAX 数据库更新
- 数据库更新的用户确认 - php + Ajax
- 如何使用 typicode/lowdb 文件数据库更新项目数组
- 如何在数据库更新时向客户端发送警报,如facebook聊天
- 在数据库更新时通过Jquery Ajax跟踪和操纵内容
- 付款处理成功,但数据库更新失败
- AJAX数据库更新与CSS按钮颜色改变使用Codeigniter
- 我怎么能得到一个文本字段的值从数据库更新的形式
- Angular JS从数据库更新视图
- 使用外部数据库更新多个站点上的html和css
- 如何在不重新加载页面的情况下使用数据库更新游戏的最高分?
- 当数据库更新时,web应用程序中的通知
- php/javascript在数据库更新后更改内联标签
- 从远程数据库更新Kendo UI Mobile中的列表视图
- 如何处理Angular控制器中的错误从MongoDB数据库更新/删除在Express
- onclick提交上的数据库更新字段不起作用