I am making an app with the MEAN stack and I am having trouble POSTing data to my database. When I push the submit button, the form data is being cleared, like it is supposed to when the POST is sucessful, but when I go to the database, there is a new document with nothing in it but and id and __v.
There aren't any errors that are being thrown because there is data being posted to the DB, but not the correct data (Look at the mongodb document at the bottom of the page)
The form where the data is being posted to is this:
<input name="userName" type="String" class="form-control" id="userName" ng-model="formData.userName" placeholder="Your Name">
<input name="game" type="String" class="form-control" id="game" ng-model="formData.game" placeholder="The game?">
<input name="userPoint" type="Number" class="form-control" id="userPoint" ng-model="formData.userPoint" placeholder="Points?">
<button type="submit" class="btn btn-default" ng-click="createScore()">Submit</button>
This is createScore():
$http.post('/api/scores', $scope.formData)
.success(function(data) {
$scope.formData = {};
$scope.scores = data;
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
This is the Mongoose model for the database:
var Score = mongoose.model("Score", {
userName: String,
game: String,
userPoint: Number
}, "score");
And this is the route for my express's "app.post()":
app.post("/api/scores", function(req, res){
Score.create({
userName:req.body.userName,
game:req.body.game,
userPoint:req.body.userPoint
}, function(err, score) {
if (err)
res.send(err);
Score.find(function(err, score) {
if (err)
res.send(err)
res.json(score);
});
});
});
When the data is POSTed, the mongodb file document looks like this:
{
"_id": {
"$oid": "54cbf3d1fbaaf10300000001"
},
"__v": 0
}
I was thinking that maybe the names were wrong (ex. userPoints and userPoint), but I made sue that they were all the same. Also, I'm new to this, so if you have any tutorials that might help concrete a topic, please feel free to share.
EDIT 1:
I did a console.log() on the node.js part where it says req.body.userName, req.body.game, etc. And in the logs, they all came out as undefined. I have been looking for a solution online, but I can't seem to find a solution. I have tried making the types in the HTML file "String" instead of "text", but that did not seem to work. Any ideas?