MongoDB / Node.js -网站崩溃,但数据库更新

MongoDB / Node.js - Website crashes but database updated

本文关键字:数据库 更新 崩溃 网站 Node js MongoDB      更新时间:2023-09-26

我以前做过一个类似的帖子,但我还没有修复它。

首先,这是我的终端:
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的评论