Meteor / MongoDB: insert integer instead of string

2019-06-22 09:10发布

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 = ""
  }
});

1条回答
贼婆χ
2楼-- · 2019-06-22 09:53

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;

查看更多
登录 后发表回答