Home > Blockchain >  I'm not getting the errors back using axios
I'm not getting the errors back using axios

Time:06-26

I'm not getting errors even though I have used validate() method using axios.

This is my controller,

public function store(Request $request)
{
      $fields = $request->validate([
         'product_name' => 'required',
        'product_cost' => 'required | integer',
        'product_selling' => 'required | integer',
        'product_stock' => 'required | integer',
 ]);

Product::create([
    'name' => $fields['product_name'],
    'cost' => $fields['product_cost'],
    'selling' => $fields['product_selling'],
    'stock' => $fields['product_stock'],
]); }

This is my Vue file

    const productState = reactive({
        product_name: "",
        product_cost: "",
        product_markup: "",
        markup_type: "Markup by price",
        product_selling: "",
        product_stock: "",
        returned_errors: [],
    });

   axios .post("api/products", productState)
    .then((response) => {
        console.log(response);
    })
    .catch((error) => console.log(error.response.data.errors));

Even if it has errors still it returns the response.

CodePudding user response:

Did you try with:

error.response.data.error

or just log error and see what is the structure

CodePudding user response:

The returned error in Axios not always has the error.response property. You probably should structure your error handling as in this example in the Axios docs.

Hope this helps.

  • Related