【发布时间】:2017-07-02 06:18:35
【问题描述】:
我需要使用 mongoose 删除我的 mongodb 上的一条记录。
这是我的组件
deleteProduct(product){
this._confirmationService.confirm({
message: 'Are you sure you want to delete the item?',
accept: () => {
this._productsAdminService.deleteProduct(product._id)
.subscribe(products => {
products.forEach(function(product){
if(product.cat_id === 1) product.catName = 'Dota Shirts';
if(product.cat_id === 2) product.catName = 'Gym Shirts';
if(product.cat_id === 3) product.catName = 'Car Shirts';
});
this.products = products;
},
err => console.log(err));
}
})
}
基本上这只会将产品 ID 传递给服务以执行 http 请求。
这是我的服务
deleteProduct(productId){
let headers = new Headers({'Authorization': 'JWT ' + localStorage.getItem('currentUserToken')});
let options = new RequestOptions({ headers: headers});
return this._http.delete('http://localhost:3000/admin/products/delete/' + productId, options)
.map((response: Response) => response.json())
.catch(this._handlerError);
}
我正在使用 delete 方法在 expressJS 中调用我的 API。
这是我的 API
productsAdminRouter.route('/delete/:productId')
.delete(function(req,res){
id = req.params.productId;
console.log(id);
Products.findByIdAndRemove(id)
.exec(function(err, done){
if (err) throw err;
Products.find()
.exec(function(err, products){
res.json(products);
});
});
});
但我总是遇到这个错误
有人可以帮忙吗?我被卡住了。
【问题讨论】:
标签: mongodb angular express mongoose