MongoDB 未使用 Mongoose 发布任何数据
MongoDB is posting no data with Mongoose
我正在使用 MEAN 堆栈制作一个应用程序,但我在 POST 向我的数据库中输入数据时遇到了问题。当我按下提交按钮时,表单数据被清除,就像 POST 成功时应该的那样,但是当我进入数据库时,有一个新文档,里面什么都没有,只有 id 和__v.
没有任何错误被抛出,因为有数据被发布到数据库,但不是正确的数据(查看页面底部的 mongodb 文档)
数据发布到的表单是这样的:
<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>
这是 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);
});
这是数据库的 Mongoose 模型:
var Score = mongoose.model("Score", {
userName: String,
game: String,
userPoint: Number
}, "score");
这是我的快递 "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);
});
});
});
当数据被 POSTed 时,mongodb 文件文档如下所示:
{
"_id": {
"$oid": "54cbf3d1fbaaf10300000001"
},
"__v": 0
}
我在想也许名字有误(例如 userPoints 和 userPoint),但我起诉说它们都是一样的。另外,我是新手,所以如果您有任何可能有助于具体主题的教程,请随时分享。
编辑 1:
我在 node.js 部分做了一个 console.log() ,其中显示 req.body.userName、req.body.game 等。在日志中,它们都未定义。我一直在网上寻找解决方案,但似乎找不到解决方案。我尝试将 HTML 文件中的类型设为 "String" 而不是 "text",但这似乎不起作用。有什么想法吗?
好的,我解决了我的问题。
我试图 POST 作为表单数据而不是 x-www-form-urlencoded。
在我的 core.js 中,我把它放在 createScore 方法中:
$http({
method : 'POST',
url : '/api/scores',
data : $.param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response){
$scope.formData = {};
$scope.scores = response;
console.log(response);
});
希望对大家有所帮助!
我正在使用 MEAN 堆栈制作一个应用程序,但我在 POST 向我的数据库中输入数据时遇到了问题。当我按下提交按钮时,表单数据被清除,就像 POST 成功时应该的那样,但是当我进入数据库时,有一个新文档,里面什么都没有,只有 id 和__v.
没有任何错误被抛出,因为有数据被发布到数据库,但不是正确的数据(查看页面底部的 mongodb 文档)
数据发布到的表单是这样的:
<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>
这是 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);
});
这是数据库的 Mongoose 模型:
var Score = mongoose.model("Score", {
userName: String,
game: String,
userPoint: Number
}, "score");
这是我的快递 "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);
});
});
});
当数据被 POSTed 时,mongodb 文件文档如下所示:
{
"_id": {
"$oid": "54cbf3d1fbaaf10300000001"
},
"__v": 0
}
我在想也许名字有误(例如 userPoints 和 userPoint),但我起诉说它们都是一样的。另外,我是新手,所以如果您有任何可能有助于具体主题的教程,请随时分享。
编辑 1:
我在 node.js 部分做了一个 console.log() ,其中显示 req.body.userName、req.body.game 等。在日志中,它们都未定义。我一直在网上寻找解决方案,但似乎找不到解决方案。我尝试将 HTML 文件中的类型设为 "String" 而不是 "text",但这似乎不起作用。有什么想法吗?
好的,我解决了我的问题。
我试图 POST 作为表单数据而不是 x-www-form-urlencoded。
在我的 core.js 中,我把它放在 createScore 方法中:
$http({
method : 'POST',
url : '/api/scores',
data : $.param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response){
$scope.formData = {};
$scope.scores = response;
console.log(response);
});
希望对大家有所帮助!