I have a simple registration endpoint that I want to allow for a user to register in my vue app, I also want to display appropriate errors from my backend to the vue client.
The error JSON structure looks like this:
{
"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 am able to log out the error response like this
I try logging out my errors in my Form component but now I am just getting an empty object (I am using reactive to handle this error object in my composable)
2
Answers
It seems like you are returning an array, not an object.
So to get access, you would do
errors[0].Password
.Did you intend to use an object or an array (might be useful if you have mutliple errors)?
If the array is intended and you need to check all errors for the
Password
property, you would do something like this:Reflection on your code
Multiple errors are present in it, making it difficult for me to provide a concise answer that suits your needs. Instead, I quickly put together a code snippet that works based on your principles. Going forward, I will try to highlight what to watch out for and provide some good advice for the future.
– Use
async function()
to can waiting response ofPromise
BAD (if you want to use the result immediately, currently
console.log
justifies usingasync
)This doesn’t provide an immediate result; it requires some time to run on a separate thread. As a result, the JavaScript script moves forward almost immediately. Typically, if you want to use the result immediately, this will result in an error because the response hasn’t arrived yet.
So when you try to access the new result of
errors
, you see that it’s empty, even though after theconsole.log
, after 1-2 seconds, it won’t be empty anymore becauseregister()
has finished executing.OK
await
– Wait process end – MDN Docsasync function
– What need toawait
using – MDN Docs– How to use
ref()
on VueJS?1.
Store the values held by
ref()
,reactive()
,computed()
, etc. in variables that cannot be modified. Always use const when declaring these variables.More information – StackOverflow Answer
BAD
OK
2.
You use the
.value
suffix in one instance and not in another. Well, the situation is that the result ofref()
variables is always stored in.value
. You can manipulate it accordingly.BAD
OK
How to use
ref()
? – VueJS DocsSample code with the outlined logic
I commented the lines for better code comprehension. I hope it’s understandable.
I had to complement your code with a few demo functions and implemented the vue.js CDN version to make the sample code workable. Of course, you only need to review the relevant parts in your own code to see what matches or differs. For example, I assume you didn’t create the AuthServiceClass yourself but rather a third party, so it should work correctly, but you will definitely find differences in your own Vue file.