Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor / MongoDB: insert integer instead of string on form submit

I'm working through David's tutorial on Meteor at http://meteortips.com/.

How do I insert an integer instead of string on form submit?

I think the following line needs to clarify that it's an integer, but I'm not sure how.

var playerScoreVar = event.target.playerScore.value;

Here's my whole code.

 Template.addPlayerForm.events({

  'submit form': function(event){
      event.preventDefault();
      var playerNameVar = event.target.playerName.value;
      var playerScoreVar = event.target.playerScore.value;
      PlayersList.insert({
          name: playerNameVar,
          score: playerScoreVar,
      });
      event.target.playerName.value = ""
      event.target.playerScore.value = ""
  }
});
like image 909
Agent Zebra Avatar asked Sep 27 '22 23:09

Agent Zebra


1 Answers

Just convert it to an integer prior to the insert:

var playerScoreVar = parseInt(event.target.playerScore.value, 10);

or

var playerScoreVar = Number(event.target.playerScore.value);

You can see the differences explained here;

like image 119
David Weldon Avatar answered Oct 06 '22 20:10

David Weldon