0

I am beginner web developer.

I make my first project in due and laravel 8.

My API return me error: {"message":"The given data was invalid.","errors":{"email":["Taki email ju\u017c wyst\u0119puje."], "login":["Taki login ju\u017c wyst\u0119puje."]}}

I have this code:

axios.post(this.$apiAdress + '/api/administrators?token=' + localStorage.getItem("api_token"),
        self.record
      )
        .then(function (response) {
          if (response.data.status == 'success') {
            Swal.fire(
              'Sukces',
              'Rekord dodany poprawnie!@',
              'success'
            )
          } else {
            Swal.fire(
              'Błąd',
              response,
              'error'
            )
          }
          this.$router.replace({path: '/administrators/create'});
        }).catch(function (error) {
        if (error.response.data.message == 'The given data was invalid.') {
          Swal.fire(
            'Błąd',
            'Proszę wypełnić wszystkie pola oznaczone gwiazdką!',
            'error'
          )
          window.scrollTo({top: 0});
        }

In linę: error.response.data.message I have this errors.

I need print errors in Swal:

Swal.fire(
                'Błąd',
                '.... Here errors ....',
                'error'
              )

For example:

Swal.fire(
                    'Błąd',
                    'Taki email ju\u017c wyst\u0119puje. <br/> Taki login ju\u017c wyst\u0119puje.',
                    'error'
                  )

How can I make it?

Please help me

3 Answers 3

1

You can just iterate over the errors object:

if (error.response.data.message == 'The given data was invalid.') {
    let error_details = ""

    for (let key in error.response.data.errors) {
        error_details += `${error.response.data.errors[key]}<br/>`
    }

    Swal.fire(
        'Błąd',
         error_details,
        'error'
    )
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

This should work in this specific example:

Swal.fire(
  'Błąd',
  error.response.data.email + "<br />" + error.response.data.login,
  'error'
)

1 Comment

I can't use this form: response.error.email because I don't know how many errors user can generated. Is possible make universal show this error from json: {"message":"The given data was invalid.","errors":{"email":["Taki email ju\u017c wyst\u0119puje."], "login":["Taki login ju\u017c wyst\u0119puje."]}} ?
0

You're printing an entire object which results in this:

"[object Object]"

You need to get the property FROM the object.

response.error.email
// Taki email ju\u017c wyst\u0119puje. <br/> Taki login ju\u017c wyst\u0119puje.

Or to print the object as string:

JSON.stringify(response);

You can also get the specific errors.

var errors = JSON.parse('{"message":"' + response.message + '","errors":' + JSON.stringify(response.errors) + '}');
// Object { ... }

3 Comments

I can't use this form: response.error.email because I don't know how many errors user can generated. Is possible make universal show this error from json: {"message":"The given data was invalid.","errors":{"email":["Taki email ju\u017c wyst\u0119puje."], "login":["Taki login ju\u017c wyst\u0119puje."]}} ?
Yes it is, JSON.stringify(response).
This return me: {"message":"Request failed with status code 422","name":"Error","stack":"Error: Request failed with status code 422\n at createError (webpack-internal:///./node_modules/axios/lib/core/createError.js:16:15)\n at settle (webpack-internal:///./node_modules/axios/lib/core/settle.js:17:12)\n at XMLHttpRequest.onloadend (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:66:7)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":... not error

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.