簡體   English   中英

如何獲取剛剛由 POST 創建的項目

[英]How to GET the item just created by the POST

我成功地通過 axios 獲取數據,現在我需要通過 POST 方法創建的 Axios 獲取數據。 這是我的代碼:

  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);
    },

控制台日志(this.selected_item.id); 返回 0 它應該返回類似 69843 的東西,id 是自動生成的。

您的 POST api 調用應創建該項目,然后返回新創建的項目的 ID。 然后您可以執行以下操作。

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);
        });
    })
  }

如果您只需要單行,我還認為您應該對單個項目進行 GET 調用。 http://localhost:8080/bills/{id}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM