I have a simple registration endpoint that I want to allow users to register in my vue application, I also want to display appropriate errors to the vue client from my backend.
The error JSON structure is as follows:
{ "errors": { "Password": [ "Password is required", "Minimum length of 8 characters is required" ], "UserName": [ "Username is required", "Minimum length of 6 characters is required" ], "EmailAddress": [ "Email Address is required", "Invalid Email Address" ], "ConfirmPassword": [ "Confirm Password is required" ] } }
I have a composable with a register function like this:
export default function useAuth() { let errors = ref({}) const register = (request) => { errors = {} AuthService.register(request) .then(res => { console.log("res: "+ res) }) .catch(err => { const errList = err.response.data.errors; errors = errList // Here i'm getting a reponse console.log(errors) }) } return { register, errors } }
I also have a form component which is just a simple form with v-models added:
<script> // Imports.. export default { components: {}, setup() { const { register, errors} = useAuth(); const request = { userName: "", emailAddress: "", password: "", confirmPassword: "", }; const handleSubmit = () => { register(request); // empty object console.log(errors) }; return { errors, request, handleSubmit }; }, }; </script>
In my composable I can log out the error response like this
Error response
I tried unregistering the error in the form component but now I just get an empty object (I'm using reactive to handle this error object in the composable)
Empty object response from composable items
Looks like you are returning an array, not an object.
So to gain access you need to execute
errors[0].Password
.Are you going to use an object or an array (might be useful if you have multiple errors)?
If the array was expected and you needed to check the
Password
property for all errors, you would do something like this:Reflect on your code
There are multiple errors in it, making it difficult for me to provide a concise answer that fits your needs. Instead, I quickly put together a code snippet that works according to your principles. Going forward, I'll try to highlight things to watch out for and provide some good advice for the future.
- Use
async function()
to wait for the response ofPromise
Bad (if you want to use the results immediately, currently
console.log
proves usingasync
)This does not provide results immediately; it takes some time to run on a separate thread. As a result, the JavaScript script moves forward almost immediately. Normally this will result in an error if you want to use the result immediately because the response hasn't arrived yet.
So when you try to access the new result for
errors
you will see it is empty even afterconsole.log
after 1-2 seconds it It will no longer be empty becauseregister()
has been executed.OK
Wait
- Wait for process to end - MDN DocumentationAsynchronous functions
- What is neededawait
Using - MDN Documentation- How to use
ref()
on VueJS?1.
Store the saved values of
ref()
,reactive()
,compulated()
, etc. in variables that cannot be modified. Always use const when declaring these variables.More Information - StackOverflow Answers
not good
OK
2.
You use the
.value
suffix in one instance but not in another. Well, what happens is that the result of aref()
variable is always stored in.value
. You can manipulate it accordingly.not good
OK
How to use
ref()
? - VueJS DocumentationSample code with outlined logic
I have commented these lines to understand the code better. I hope this is understandable.