简体   繁体   中英

How to GET the item just created by the POST

I successfully managed to GET data by axios, now I need to get data by Axios which was just created by POST method. Here is my code:

  data() {
    return {
      showModal: false,
      info: null,
      percentage: 0,
      selected_item: {
        id: 0,
        bill_number: 0,
        date: "",
        user_id: 0,
      },
    };
  },
    getNewDeal() {
      axios.post("http://localhost:8080/bills", {
        bill_number: 108,
        date: "",
        user_id: 7,
      });
      axios
        .get("http://localhost:8080/bills")
        .then((response) => (this.info = response.data._embedded.bills));
      this.info[this.info.length - 1].id = this.selected_item.id;
      console.log(this.selected_item.id);
    },

console.log(this.selected_item.id); returns 0 it should return something like 69843, id is autogenerated.

Your POST api call should create the item and then return the ID of the newly created item. Then you can do the following.

getNewDeal() {
    axios.post("http://localhost:8080/bills", {
      bill_number: 108,
      date: "",
      user_id: 7,
    }).then((response) => { // This response should have the ID of the newly created item, this should be set in the POST api call after item create.
      this.selected_item.id = response.data.id // I dont know your response object but get the ID from the response.
      axios
        .get("http://localhost:8080/bills")
        .then((response) => {
          this.info = response.data._embedded.bills;
          this.info[this.info.length - 1].id = this.selected_item.id;
          console.log(this.selected_item.id);
        });
    })
  }

I would also think you should have a GET call for a single item if all you need is the single row. http://localhost:8080/bills/{id}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM