繁体   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