0

I'm having problems with promise response for a vForm PUT to UPDATE a model (backend in laravel).

The response code is 200 (OK, updated) and the model is updated, but I don't know why I'm having error with "response.data" in catch. There is no error and code in ".then()" is running correctly.

EDIT

Service Update funciton (vue) using vForm. 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');
      },

Function in backend (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);
    }
}

enter image description here

enter image description here

enter image description here

enter image description here

3
  • What is the result of you console.log? Commented Aug 18, 2019 at 14:50
  • You need to provide additional information. Are you using axios to make HTTP calls? Are you extracting data in service in success? You need to need to share the code for this.service.put. Also don't put images of your code into the question, paste the actual code into the question. Commented Aug 18, 2019 at 15:01
  • @AlexanderStaroselsky the post is updated with new information. Commented Aug 18, 2019 at 15:19

1 Answer 1

1

This line looks problematic to me:

this.$Progress.finish();

It's trying to access this within the function passed to then. It seems unlikely that this will be referencing what you're expecting. You should be able to confirm with suitable console logging. My suspicion is that attempting to call this.$Progress.finish() will throw an error, triggering the catch.

Try using arrow functions for your then and catch callbacks instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! thats was the error. Putting it out solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.