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.jsreq.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、 byconsole.log( 'body: ', req.body );

Jquery - $.post() で contentType=application/json を使用する方法は?

POST クエリ パラメータを取得する方法は? 、特にSooDesuNeからのコメント

于 2012-12-24T00:06:47.940 に答える