Я пытаюсь обновить список Объектов на MongoDB всего одним вызовом метода (для внутри Submit), но это не работает .. Я вчера провел целый день и не мог заставить его работать .. У меня естьпродукт в базе данных, и я хочу обновить все продукты с продажами (у меня есть форма со всеми продуктами и поле ввода с продажами, я заполняю их все и нажимаю Сохранить).
Внешний интерфейс: угловойСерверная часть: Узел (с экспрессом) База данных: MongoDB (mLab.com)
productSells.component.html (входные данные Sells):
<input type="number" id="sells" class="form-control"
formControlName="sells" value="{{ product.sells }}">
productSells.component.ts
products: Product[] = [];
ngOnInit(): void {
this.createFormControls();
this.createForm();
this.productService.getProducts()
.subscribe(products => {
this.products = products;
},
error => this.errorMessage = <any>error);
}
createFormControls() {
this.sells = new FormControl(0, [
Validators.required
]);
}
createForm() {
this.sellsForm = new FormGroup({
sells: this.sells,
});
}
sellsSubmit() {
for (const prod of this.products) {
const product = new Product(
this.sells.value
);
this.router.navigate([`/products/sells/${prod._id}`]);
this.productService.updateProduct(prod._id, product);
}
location.reload();
}
Узел:
iceRouter.route('/products/sells/:productId')
.put((req, res) => {
IceCream.findById(req.params.productId, (err, iceC) => {
if (err) {
res.status(500).send(err);
}
iceC.sells = req.body.sells;
iceC.save();
res.json(iceC);
})
})
product.service.ts - updateProduct ():
updateProduct(pId: string, product: Product): Observable<any> {
return this._http.put<Product>(`${this._productUrl}/${pId}`, product, this.httpOptions);
}
Если я использую почтальон с продуктом _id, я могу обновить продажи, но не могупо заявке .. Любая помощь будет принята с благодарностью.Спасибо.
@ Edit - добавлена последняя версия кода:
productSells.component.html
<input name="sells{{i}}" [(ngModel)]="products[i].sells"
type="number" class="form-control" id="sells"
formControlName="sells" value="{{ product.sells }}">
productSells.component.ts
sellsSubmit() {
for (let i = 0; i < this.products.length; i++) {
const product = new Product(
this.products[i].sells
);
this.router.navigate([`/products/sells/${this.products[i]._id}`]);
this.productService.updateProduct(this.products[i]._id, product).subscribe(() => {
console.log('sellsSubmit done!');
});
}
location.reload();
}
Коды:
ProductSells.component.html
ProductSells.component.ts
Product.service.ts