You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

删除记录时遇TypeError: Cannot read properties of undefined (reading 'then')错误求助

解决删除数据时的TypeError: Cannot read properties of undefined (reading 'then')问题

这个错误的核心原因是你调用.then()的对象不是Promise实例,具体到你的代码里,问题出在SweetAlert版本不支持Promise链式调用。

具体解决方法分两种情况:

情况1:使用旧版SweetAlert(1.x版本)

旧版SweetAlert不返回Promise,无法用.then()处理确认逻辑,需改用回调参数写法:

$('.delete').click(function () {
  var deleteId = $(this).data('id');
  
  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#4fa7f3',
    cancelButtonColor: '#d57171',
    confirmButtonText: 'Yes, delete it!',
  }, function (isConfirmed) { // 用回调替代.then()
    if (isConfirmed) { // 旧版直接返回布尔值,无需result.value
      axios
        .post('{{route("company.destroy")}}', {
          _method: 'delete',
          _token: '{{csrf_token()}}',
          deleteId: deleteId,
        })
        .then(function (response) {
          console.log(response);
          swal(
            'Deleted!',
            'User has been deleted.',
            'success'
          );
          table.draw();
        })
        .catch(function (error) {
          console.log(error);
          swal(
            'Failed!',
            error.response.data.error,
            'error'
          );
        });
    }
  });
});

情况2:保留Promise链式调用风格

升级到SweetAlert2(推荐),它原生支持Promise API,升级后只需调整确认结果的判断逻辑:

$('.delete').click(function () {
  var deleteId = $(this).data('id');
  
  Swal.fire({ // SweetAlert2调用方式为Swal.fire()
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    icon: 'warning', // 参数名从type改为icon
    showCancelButton: true,
    confirmButtonColor: '#4fa7f3',
    cancelButtonColor: '#d57171',
    confirmButtonText: 'Yes, delete it!',
  }).then(function (result) {
    if (result.isConfirmed) { // 用isConfirmed判断确认操作
      axios
        .post('{{route("company.destroy")}}', {
          _method: 'delete',
          _token: '{{csrf_token()}}',
          deleteId: deleteId,
        })
        .then(function (response) {
          console.log(response);
          Swal.fire(
            'Deleted!',
            'User has been deleted.',
            'success'
          );
          table.draw();
        })
        .catch(function (error) {
          console.log(error);
          Swal.fire(
            'Failed!',
            error.response.data.error,
            'error'
          );
        });
    }
  });
});

额外检查点

  • 确保axios已正确引入并在当前作用域全局可用。

内容的提问来源于stack exchange,提问作者Malik Zubair Mukhtar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 07:07:46