【发布时间】:2016-03-30 07:44:44
【问题描述】:
谁能告诉我为什么我的 POST 方法没有通过 Mongoose 保存到我的 MongoDB 中?
我的 Angular 控制器
$scope.saveUpdate = function(id){
$http.post('/api/entry/' + id)
.success(function(data){
$scope.entry = data;
})
.error(function(data){
console.log('There was a problem saving your entry: ' + data);
});
// update page with remaining entries
$http.get('/api/entries').then(function(response){
$scope.entries = response.data;
});
}
我的 API
app.post('/api/entry/:entry_id', function(req, res){
if (req.params) {
Entries.findByIdAndUpdate({
_id : req.params,
// the properties we're updating and the new values
username: req.body.username,
date: req.body.date,
income: req.body.income
}, function(err, entry){
if (err) {
res.send(err) }
else {
res.send('Success!');
}
})
}
});
视图中的提交按钮
<button type="submit" class="btn" ng-click="saveUpdate(entry._id)">Update</button>
当点击按钮时,更新的条目会到达 DOM,但当它到达 Angular 核心代码时,它会恢复到原始状态而不更新数据库。也不会抛出任何错误。
【问题讨论】:
标签: angularjs node.js mongodb express mean-stack