0

I made a post before slightly similar, but I have not fixed it yet.

First, here is my terminal:

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)

So it is successfully creating an entry in the database which is good, but after this when it tries to redirect back to '/team', the page crashes which is on this:

index.js routes file

/**
  * 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
      });
    });
  });

I made some progress since before, I have added bodyParser() to the app.js file which I hadn't done before, but I am told it is crashing because req.body is not being parsed or it is being taken in as the wrong format.

Here is also the team.js page:

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

Any ideas how to fix this?

germainelol
  • 3,231
  • 15
  • 46
  • 82
  • you said it crashes when redirecting, post the code where that redirection happens – soulcheck Dec 23 '12 at 23:44
  • I'm sure it's more to do with `teamForm` within the `index.js` file. The data it's recieving there is `undefined` when i print to console, which i dont quite understand as it still creates a new database entry – germainelol Dec 23 '12 at 23:54

1 Answers1

0

The problem is with your $.post call, it should send the data with contentType=application/json header. don't ask why?

so if you change it to something like this:

$.ajax({
  url:'/team',
  type:"POST",
  data: JSON.stringify({teamFrom:teamFrom}),
  contentType:"application/json; charset=utf-8",
  dataType:"json",
  success: function(response){
    console.log(response);
  }
})

on the node side, for debugging chekc what's in the req.body, by console.log( 'body: ', req.body );

more reading Jquery - How to make $.post() use contentType=application/json?

and

How to retrieve POST query parameters?, specially a comment from SooDesuNe

Community
  • 1
  • 1
balazs
  • 5,698
  • 7
  • 37
  • 45
  • Hi, interesting posts, I have tried using the ajax call POST you wrote, but this still didnt work. The new teams are added to the database successfully, but `req.body` is printed as `undefined` in console. I'm still not sure why, surely it should work with your ajax code – germainelol Dec 24 '12 at 00:20
  • to be more precise it prints `body: {}` sorry But `req.body.teamForm.name` does print the team name – germainelol Dec 24 '12 at 00:25
  • The problem is only occuring when the page is reloaded by the way, I've just noticed this – germainelol Dec 24 '12 at 00:29