我有以下代码:
router.put('/:id', function(req, res, next) {
Produscts.findByIdAndUpdate(req.params.id, {$set:req.body}, function (err, post) {
if (err) return next(err);
res.json(post);
});
});
以下是我的服务代码以更新数据:
updateProducts(productForm, id) {
let uri = http://localhost:3000
const temp = {
'description': productForm.description,
'quality': productForm.quality
};
return this.httpObj.put(`${this.uri}/products/${id}`, JSON.stringify(temp), {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
我的第一个问题是我无法获得与_id属性匹配的数据。其次,以更新形式输入的数据不会在我的数据中更新,而只会导航到主页。
ts代码:
_id: number;
product: Object = {};
products;
constructor(private productService: ProductTrackerService, private route: Router, private routes: ActivatedRoute ) { }
ngOnInit() {
this.routes.params.subscribe(params => {
this._id = +params['id'];
});
this.productService.getProducts()
.subscribe((data: Response) => {
this.products = JSON.stringify(data);
this.product = JSON.parse(this.products);
for (let i in this.product) {
if (this.product[i]._id === (this._id)) {
this.product= this.product[i];
}
}
});
}