【问题标题】:TypeError: response.data is undefined类型错误:response.data 未定义
【发布时间】:2019-12-24 00:25:41
【问题描述】:

我在使用 vForm PUT 更新模型(laravel 中的后端)的承诺响应时遇到问题。

响应代码为 200(OK,已更新)并且模型已更新,但我不知道为什么在 catch 中出现“response.data”错误。没有错误,“.then()”中的代码运行正常。

编辑

使用 vForm 的服务更新功能 (vue)。 更新服务(){ this.$Progress.start();

        this.service.put('api/service/' + this.service.id)
        .then( function (response) {

          Toast.fire({
            type: 'success',
            title: response.data['Response']
          });

          this.$Progress.finish();

        })
        .catch( function (response) {
          console.log(response);
          Swal.fire("Error!", response.data['Response'], "warning");
          this.$Progress.fail();

        });

        this.$events.$emit('ServiceInform');
      },

后端函数(laravel)。

public function update(Request $request, Service $service)
{
    $this->validate($request, [
        'id_customers'      => 'required|int',
        'date'              => 'required|date',
        'id_technicians'    => 'required|int',
        'location'          => 'required|string',
        'details'           => 'required|string'
    ]);

    if ($request['id_technicians'] !== $service['id_technicians']) {
        $assignated_by = Auth::user()->id;
        $assigned_date = date('Y-m-d H:i:s');
    } else {
        $assignated_by = $service['assignated_by'];
        $assigned_date = $service['assigned_date'];
    }

    if ($request['id_technicians'] == 0) {
        $state = 'P';
    } else {
        $state = 'I';
    }

    $service->date              = $request['date'];
    $service->id_technicians    = $request['id_technicians'];
    $service->location          = $request['location'];
    $service->details           = $request['details'];
    $service->assigned_date     = $assigned_date;
    $service->assigned_by       = $assignated_by;
    $service->state             = $state;

    try {

        $service->save();

        return Response::json([
            'Response' => 'Servicio actualizado.'
        ], 201);

    } catch (Exception $e) {
        return Response::json([
            'Response' => 'No se actualizó el servicio.'
        ], 422);
    }
}

【问题讨论】:

  • console.log的结果是什么?
  • 您需要提供其他信息。你在使用 axios 进行 HTTP 调用吗?您是否成功提取了service 中的data?您需要分享this.service.put 的代码。也不要将代码的图像放入问题中,将实际代码粘贴到问题中。
  • @AlexanderStaroselsky 该帖子已更新为新信息。

标签: vue.js vuejs2


【解决方案1】:

我觉得这条线有问题:

this.$Progress.finish();

它试图在传递给then 的函数中访问thisthis 似乎不太可能引用您的期望。您应该能够使用合适的控制台日志记录进行确认。我怀疑尝试调用this.$Progress.finish() 会引发错误,触发catch

尝试为您的 thencatch 回调使用箭头函数。

【讨论】:

  • 谢谢!!那是错误。把它拿出来解决问题。
猜你喜欢
  • 2019-10-31
  • 2016-08-07
  • 2019-01-07
  • 2020-01-28
  • 2021-10-24
  • 2014-10-17
  • 2022-01-10
  • 2020-02-27
  • 2012-09-12
相关资源
最近更新 更多