我在 vForm PUT 更新模型(laravel 中的后端)的承诺响应方面遇到问题。
响应代码是 200(好的,已更新)并且模型已更新,但我不知道为什么我在 catch 中出现“response.data”错误。“.then()”中没有错误并且代码运行正常。
使用 vForm 的服务更新功能 (vue)。updateService(){ 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);
}
}
这条线在我看来有问题:
this.$Progress.finish();
它试图this
在传递给then
. 似乎不太可能this
引用您的期望。您应该能够通过适当的控制台日志记录进行确认。我怀疑尝试调用this.$Progress.finish()
会引发错误,从而触发catch
.
尝试为您的then
和catch
回调使用箭头函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句