当尝试使用mongoose删除或编辑Mongodb上的文档时,在删除/编辑原始文档之前会创建一个重复的文档。此新文档具有新的_id。
后端路线
router.delete("/:id", (req, res, next) => {
const id = req.params.id;
Customer.deleteOne({ _id: id })
.exec()
.then(result => {
res.status(200).json({
message: "Deleted Successfuly"
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
})
});
});
router.put("/:id",(req, res, next) => {
const cust = new Customer ({
_id: req.params.id,
name: req.body.name,
address: req.body.address,
city: req.body.city,
payment: req.body.payment
});
Customer.updateOne({_id: req.params.id}, cust)
.exec()
.then(result => {
console.log(result);
res.status(200).json({message: "update success"})
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
})
});
});
从服务中截取
deleteCustomer(customerId: string) {
this.http.delete('http://localhost:3000/api/customers/' + customerId )
.subscribe((response) => {
console.log(response);
console.log(this.customers);
this.getCustomers();
});
}
editCustomer(id: string, editedCustomer: Customer) {
this.http.put('http://localhost:3000/api/customers/' + id, editedCustomer)
.subscribe((response) => {
console.log(response);
this.getCustomers();
});
}
我没有收到任何错误,并收到了成功状态消息。
关于我为什么要重复的任何想法?
随时请求其他任何相关数据。