Elementos pasaron por un formulario no recibido por el servidor ni mostrado en el frente. Método de PARCHE | Angular | Dropwizard
l lado del servidor muestra que se llama al método PATCH y agrega un valor nulo al final de la matriz, en lugar de una nueva matriz de artículos con el nombre y el precio tomados de la entrada del usuario.
étodo @PATCH en el componente de servicio:
patchAdd(menu: MenuModel | number, itemToAdd: ItemClass): Observable<any> {
const id = typeof menu === 'number' ? menu: menu.id;
const url = `${this.menusUrl}/add/${id}`;
return this.http.patch(url, itemToAdd, httpOptions).pipe ()
}
unción @patchItAdd que implementa el método patchAdd y toma datos de html: Tenga en cuenta que hay un par de comentarios, de mis intentos fallidos.
patchItAdd(name: string, price: number){
name = name.trim();
if (!name) {return;}
const id = +this.route.snapshot.paramMap.get('id');
this.menuService.patchAdd(id, this.item)
//With this ^^ , the value is null, but new element is added to the array. Cannot read property push of undefined. PATCH is triggered on the backend.
// this.menuService.patchAdd(id, {name, price} as ItemClass) ----- Three errors, nothing happens
.subscribe(item => {
this.items.push(item);
});}
tiqueta @HTML que se supone que recopila la entrada del usuario y pasa datos a la función:
<label class="forma">
Name of the item:
<input #itemName placeholder="What's the name of the item?" onfocus="this.placeholder =''" onblur="this.placeholder = 'What\'s the name of the item?'"/><br><br>
Item's price:
<input #itemPrice placeholder="What's the price?"onfocus="this.placeholder =''" onblur="this.placeholder = 'What\'s the price?'"/><br><br>
<span class="addnewbuttontext"><button class="addnewitembutton" (click) = "patchItAdd(itemName.value, itemPrice.value)">Add</button></span>
</label><br>
étodo @PATCH en el backend (Dropwizard):
@PATCH
@Path("/add/{menuId}")
public void addMenuItem(
@PathParam("menuId") final int menuId,
final Item item) {
final java.util.List<Item> items = this.menuRepository.get(menuId).getItems();
items.add(item);
}
Solo se agrega un valor nulo a la matriz en el backend después de hacer clic en el botón "AGREGAR" y llamar a la función patchItAdd. Obviamente, no se muestran datos en el front-end ni por eso. ¿Qué estoy haciendo mal, o al menos cómo debo abordar el método PATCH en el front-end, con Angular 7?