【问题标题】:Axios.put inside Axios.getaxios.put里面的axios.get
【发布时间】:2018-05-31 15:34:28
【问题描述】:

我想更新状态,但是当我想做'put'时它不起作用,因为 axios.put 中的 axios 没有定义。你能帮帮我吗?

getAbsencesByRequestId(reqId) {
    axios.get(REQUESTID_URL + reqId).then(response => {
        this.collaboId = response.data[0].collabId; 
        this.beginDate = response.data[0].startDate;
        this.finishDate = response.data[0].endDate;
        this.reason = response.data[0].type;
    }, (error) => {
        console.log(error.response.status)
    }) axios.put(REQUEST_URL + reqId, {
        collabId: this.collaboId,
        startDate: this.beginDate,
        endDate: this.finishDate,
        status: 'VALIDATED',
        type: this.reason
    })
},

【问题讨论】:

  • 请格式化,使其不在一行中。
  • 我认为您向我们展示的代码没有任何问题。你确定axios.put 是未定义的吗?在我看来,REQUEST_URL 更可能是未定义的,因为您之前使用的是 REQUESTID_URL。否则,您可能需要向我们展示您从哪里获得 axios,因为 require('axios') 确实有 .put 方法。
  • 还值得注意的是,您并没有在“内部”axios.get 执行此操作。您在 axios.get 之后执行此操作,而且您甚至没有等待承诺解决。也就是说,时间线将是 1. 发出 Get 请求 2. 发出 Put 请求 3. 响应 Get 或 put 请求。

标签: javascript vue.js axios


【解决方案1】:

你应该妥善管理你的请求的顺序

axios.get(url /*optional payload and headers*/).then((getResponse) => {
  //do GET stuff with response
}).then(() => {
  //do PUT call
  axios.put(url, /*optional payload and headers*/).then((putResponse) => {
    //do PUT stuff with response
  })
}).catch((e) => {
  //handle the error
})

【讨论】:

    猜你喜欢
    • 2018-08-05
    • 2023-03-12
    • 1970-01-01
    • 2011-03-11
    • 2020-11-10
    • 2018-11-10
    • 1970-01-01
    • 2019-02-10
    • 2023-02-20
    相关资源
    最近更新 更多