-1

I ran into problems with element-ui when I executed the promise function in the method. "res" prints out as "undefined" and jumps into ".catch", but in Network the api returns the data normally.Finally I failed to delete

deleteTradeMark(row) {
      this.$confirm(`此操作将永久删除${row.tmName}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
       let res =  await this.$API.trademark.reqDeleteTradeMark(row.id)
        console.log(res)   //  undefind
        if(res.code===200){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getTradeMark(this.list.length>1?this.page:this.page-1)
        }

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
 export  const  reqDeleteTradeMark=(id)=>{
  request({url:`/api/admin/product/baseTrademark/remove/${id}`,method:'delete'})
}

enter image description here The api returns code:"200" and delete:" Deleted successfully ".

Why do all the other promises work, is it because of ".then" ?

Tom
  • 3
  • 2
  • Just because the network shows data in the response doesn't mean your code uses it correctly. What does the `this.$API.trademark.reqDeleteTradeMark` function do? Please [edit] your question to show that code – Phil May 31 '23 at 09:07
  • It is a request method for deletion – Tom May 31 '23 at 09:21

2 Answers2

1

You should add return before request in `reqDeleteTradeMarkq method.

export  const  reqDeleteTradeMark=(id)=>{
      return request({url:`/api/admin/product/baseTrademark/remove/${id}`,method:'delete'})
}
周星星
  • 61
  • 3
  • How do I know when to add```return```, and some requests API are fine without a return。 ```export const reqTradeMarkList =(page,limit)=>request({url:`/api/admin/product/baseTrademark/${page}/${limit}`,method:'get'})``` – Tom May 31 '23 at 10:24
  • 1
    Well, this is a grammar about `arrow function`. If your `arrow function` have only one line, and you want return its value, you can write like this: `() => request('')`. It sames as `() => { return request('') }`. I recommend you to read this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – 周星星 May 31 '23 at 10:37
-1

async-await grammar act on Promise, your function reqDeleteTradeMark not return Promise async-await-grammar