0

我之前发过一个帖子,有点类似,但我还没有修复它。

首先,这是我的终端:

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();

任何想法如何解决这一问题?

4

1 回答 1

0

问题出在您的$.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);
  }
})

在节点方面,用于调试 chekc 中的内容req.body,通过console.log( 'body: ', req.body );

更多阅读 Jquery - 如何使 $.post() 使用 contentType=application/json?

如何检索 POST 查询参数?,特别是来自 SooDesuNe 的评论

于 2012-12-24T00:06:47.940 回答