Server side shows that the PATCH method is called and adds a null at the end of the array, instead of a new Item array with the Name and Price taken from user input.
PATCH method in the service component:
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 ()
}
patchItAdd function which implements the patchAdd method and takes data from html: Note that there are a couple of comments, from my unsuccessful tries.
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);
});}
HTML label which is supposed to collect user input and pass data to the function:
<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>
PATCH method in the 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);
}
Only a null is added to the array on the backend after clicking the "ADD" button and calling the patchItAdd function. Obviously, no data is displayed on the front-end neither because of that. What am I doing wrong, or at least how should I approach the PATCH method on the front-end, with Angular 7?