【发布时间】:2018-01-12 19:02:12
【问题描述】:
我想使用 angularjs 和 node.js 删除 mongodb 中的数据,但无法删除 /api/manage-product 错误出现在控制台中。
.html 文件
<tbody>
<tr ng-repeat="product in vm.result">
<td>{{ product.Product_Name }}</td>
<td>{{ product.Brand }}</td>
<td>{{ product.Color }}</td>
<td>{{ product.Price }}</td>
<td>{{ product.Rating }}</td>
<td><img style="heigth:30px; width:30px;" src='{{ product.Image }}'></img></td>
<td><button class="btn btn-danger" ng-click="remove(product._id)">Remove</button></td>
</tr>
</tbody>
controller.js
$scope.remove = function(object) {
$http({
url: 'http://localhost:7200/api/manage-product',
method: 'DELETE',
data: {_id: object.id},
headers: {"Content-Type": "application/json;charset=utf-8"}
}).then(function(res) {
console.log(res.data);
}, function(error) {
console.log(error);
});
};
node.js
router.delete('/manage-product/:_id', function(req,res){
var db = req.db;
var _id = req.params._id.toString();
var collection = db.get('proInfo');
collection.remove({"_id":_id}, function(err, result) {
res.send( (result === 1) ? { msg: 'Deleted' } : { msg: 'error: '+ err } );
});
});
【问题讨论】:
-
您的
router路径上没有/api/前缀?此外,请考虑使用$resource而不是$http与 RESTful 服务交互。