0

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?

soulzap
  • 69
  • 1
  • 1
  • 7
  • While debugging the Angular Typescript "patchAdd" function, do you see a correct value for itemToAdd? – LppEdd Feb 17 '19 at 22:23
  • i'm not at the machine currently, I will be in like 8 hours. What to do in case I do, and what in the case I don't, if you don't mind? – soulzap Feb 17 '19 at 22:31

1 Answers1

0

I'll try to address a couple of points, even if I don't have a complete understanding of the problem.

Why are putting a + there? It will transform the variable in its numeric representation, is that what you want? The get() call return value is string | null

const id = +this.route.snapshot.paramMap.get('id');

Here you're calling menuService.patchAdd with this.item, but I suppose you want to create a new Item instance using the input values name and price.

// Angular
(click) = patchItAdd(itemName.value, itemPrice.value)

// Java
patchItAdd(name: string, price: number){
     ...
     this.menuService.patchAdd(id, this.item)

Are my assumptions correct?


Another issue I see is that you're passing the id, which should be string (or number in your case, since you transform it with the +) to patchAdd

const id = +this.route.snapshot.paramMap.get('id');
this.menuService.patchAdd(id, this.item)

The patchAdd function expect a MenuModel, which is a complex object.
How's that? Are you sure this is what you want?


Let patchItAdd accept the menu.id value

(click) = "patchItAdd(menu.id, itemName.value, itemPrice.value)"

Now update the method

// Now this method accept the MenuModel#id property
patchItAdd(menuId: number, name: string, price: number){
   if (!name) {
      return;
   }

   const trimmedName = name.trim();

   // Construct a new ItemClass instance.
   // You need to take care by yourself of the "person" property and the "quantity" property
   const newItem = new ItemClass("", trimmedName, 0, price)

   this.menuService.patchAdd(menuId, newItem).subscribe(item => {
      this.items.push(item);
   });
}

Update also the patchAdd method to accept the menu ID, instead of the complete MenuModel

// Now this method accepts the menu ID  
patchAdd(menuId: number, itemToAdd: ItemClass): Observable<any> { 
   const url = `${this.menusUrl}/add/${menuId}`
   return this.http.patch(url, itemToAdd, httpOptions)
}

On the back-end add a no-args constructor to Item

public Item() { }
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • yes, you're right! I want input the name and price, pass that to the backend, add it to the array and later display it with the get method. I appreciate every bit of information and code you share with me, so please do not hesitate to post anything. – soulzap Feb 17 '19 at 22:42
  • @soulzap could you post the ItemClass Typescript class? – LppEdd Feb 17 '19 at 22:43
  • hey, take a look at this, there is a lot more code in this post. It is the same issue, but I posted another one (this one) with a lot less code, as I didn't want anyone to be overwhelmed by the amount of code. The ItemClass is here as well. https://stackoverflow.com/questions/54737419/item-names-not-loading – soulzap Feb 17 '19 at 22:45
  • @soulzap could you answer the question I added at end of the answer? – LppEdd Feb 17 '19 at 22:49
  • I'm really not sure.. I can't come up with a working PATCH function on the front-end and I'm opened to any suggestions. I went with those parameters and stuff based off of the post method. I'm not sure how should I construct the two function on the front-end... (one in the service and the other in the component) I'm not even sure if the form (label) is structed properly, as that's how I use it for the post method. – soulzap Feb 17 '19 at 22:52
  • @soulzap Let's see if with my adjustments it will be better. See updated answer. – LppEdd Feb 17 '19 at 23:09
  • will apply the code in about 7 hours, as soon as I get to the office! Thanks you so much, I will let you know how it went... If there's something you'd like to add to the entire code, please let me know, or if there are any tips you'd like to share. Have a good one man, you're the best. – soulzap Feb 17 '19 at 23:13
  • @soulzap we will continue tomorrow – LppEdd Feb 17 '19 at 23:15
  • alright, I've got that one working! Take a look at this again please: https://stackoverflow.com/questions/54737419/item-names-not-loading I've added the final class - Orders. I need to use the patch method which adds items to the order whenever I click on that order in the menu list. I'll figure out the html part with links, but if you could help me with the backend and Angular classes for that that would be amazing! Feel free to comment there. That would be the last part to my project. – soulzap Feb 18 '19 at 06:51
  • I'm trying the same approach and using the similar method but it isn't working. I think minor changes are required. I'm using the exact same PATCH method for the order. – soulzap Feb 18 '19 at 09:17
  • I've made it work haha. All that's left to do is to store the name of the user locally and i'll figure that out on myself if I can. Thank you so much for everything my man. – soulzap Feb 18 '19 at 09:39
  • @soulzap Hey, I'm sorry, I'm having a busy day at work. I'm glad that you were able to solve the problem, good job. – LppEdd Feb 18 '19 at 10:25
  • no worries man, you helped me a ton. Is it possible to set the person variable (arugment for the ItemClass) using local storage and how would one do so? – soulzap Feb 18 '19 at 10:36
  • i got that one myself as well hahah! Thank you for everything, I will never forget how much you helped me. – soulzap Feb 18 '19 at 11:34
  • Hey buddy, do you have someone to recommend me who could help me with this: https://stackoverflow.com/questions/54854010/how-to-create-and-get-started-with-embedded-apache-derby-database-in-dropwizard Or do you, by any chances have experience with embedded databases and could give me some tips? I only need a piece of advice on how to get started, I'll figure out everything else along the way. Hope you are having a nice day. – soulzap Feb 24 '19 at 17:07
  • @soulzap I'm giving a look at it – LppEdd Feb 24 '19 at 17:12
  • great! you're already kinda familiar with my code, so that's awesome! – soulzap Feb 24 '19 at 17:19