You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you remember, our `server.js` uses the `createLocaleMiddleware` to set the current locale, which lives on ```req.locale.language```.
233
+
If you remember, our `server.js` uses the `createLocaleMiddleware` to set the current locale, which lives on `req.locale.language`.
234
234
235
235
So we get that value, and for our use case, check if it is _es_ for spanish or _en_ for english (our default in case it’s neither), and load the proper messages for the language, which are added to the Express’ ‘req’ object through polyglot’s _extend_ function.
236
236
@@ -394,7 +394,7 @@ return fromEntries(errArr)
394
394
395
395
In this code, we’re receiving both the error Object created with `express-validator` (which we’ll extract from the `req` object with the `validationResult` function in a bit), and the Express’ `req` object.
396
396
397
-
We’re creating an Array from the errObj, and then, for each entry, we’re taking the string we set as the error variable, and comparing it with the keys from the translation messages, changing the string in the `errArr`_(each "err[1].msg")_ to the actual phrase in polyglot in the desired language _(each "phrase")_.
397
+
We’re creating an `Array` from the `errObj`, and then, for each entry, we’re taking the string we set as the error variable, and comparing it with the keys from the translation messages, changing the string in the `errArr`_(each "err[1].msg")_ to the actual phrase in polyglot in the desired language _(each "phrase")_.
398
398
399
399
Finally, we use the imported `fromEntries` function, to convert the Array back into an Object and return it.
400
400
@@ -419,9 +419,33 @@ next()
419
419
420
420
In this code we receive the regular `req, res, next` from Express, and we first verify if there were any errors using express-validator’s `validationResult`.
421
421
Then, we check if there are errors, and if there are any, we return them with Express’ response.
422
-
Check that return closely, as you can see, we send the results of the `translateMessages` function that is receiving the `validationErrors`, and the req object.
422
+
Check that return closely, as you can see, we send the results of the `translateMessages` function that is receiving the `validationErrors`, and the `req` object.
423
423
We also have an `else`, that when there are no validation errors, calls `next()` to continue to the next Express middleware.
424
424
425
+
### Sending the errors
426
+
So we're able to manage the errors by converting them from the string to their translated version, and packaging it in an object, ready to be sent back to the user if needed.
427
+
428
+
Now, we just need to use that file!
429
+
Let's go back to our `auth.routes.js` file and make use of this new function by importing it:
0 commit comments