У меня есть следующий код:
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];
}
}
});
}