0

I'm pretty new to front end and programming in general. This my code snippet:

addPerson() {
  //this.addInventory()

  let url = "http://localhost:8080/savefood/persons";
  axios.post(url, this.person).then(response => {
    console.log(response.data);
    
    this.updateInventory(response.data.idPerson, this.addInventory())});

  // reset input fields
  this.person = {};
},
addInventory() {
  let url = "http://localhost:8080/savefood/inventories";
  axios.post(url, this.inventory).then(response => {
    console.log(response.data)});

  // reset input fields
  this.inventory = {};
},

What I'm trying to do is to return from addInventory()an idInventory which will be created with the axios.post().I'm failing at all my attempts. Is there a way to extract the idInventory from the response and then return it? My goal is to return the idInventory from addInventory()so that updateInventory(...) is able to work properly. If I simply replace this.addInventory() in updateInventory(..) with an id of an existing inventory it will work fine. But I want to be able to create an inventory and a person and then attach the just created inventory to the person. Thanks for your help!

  • Your `addInventory` function needs to return a `Promise` which you will resolve inside `addPerson` to get `idInventory`. – codemonkey Dec 10 '20 at 16:30
  • If you don't mind using [async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) you could do something like [this](https://gist.github.com/3limin4t0r/c45b95c9fc48ddede559f1cabc5c863a). – 3limin4t0r Dec 10 '20 at 16:46

0 Answers0