From 014e63a5dbf7797aadd0fecd434f4e2c91017576 Mon Sep 17 00:00:00 2001 From: Hamish Willee Date: Tue, 25 Jul 2023 17:13:41 +1000 Subject: [PATCH] Update index.md - move the comment up above the code --- .../express_nodejs/forms/create_genre_form/index.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/files/en-us/learn/server-side/express_nodejs/forms/create_genre_form/index.md b/files/en-us/learn/server-side/express_nodejs/forms/create_genre_form/index.md index 8eafe36c59c6599..9a7c0e644c86e40 100644 --- a/files/en-us/learn/server-side/express_nodejs/forms/create_genre_form/index.md +++ b/files/en-us/learn/server-side/express_nodejs/forms/create_genre_form/index.md @@ -128,8 +128,10 @@ asyncHandler(async (req, res, next) => { }); ``` -If the genre name data is valid then we check to see if a `Genre` with the same name already exists (as we don't want to create duplicates). -If it does, we redirect to the existing genre's detail page. +If the genre name data is valid then we perform a case-insensitive search to see if a `Genre` with the same name already exists (as we don't want to create duplicate or near duplicate records that vary only in letter case, such as: "Fantasy", "fantasy", "FaNtAsY", and so on). +To ignore letter case and accents when searching we chain the [`collation()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.collation()) method, specifying the locale of 'en' and strength of 2 (for more information see the MongoDb [Collation](https://www.mongodb.com/docs/manual/reference/collation/) topic). + +If a `Genre` with a matching name already exists we redirect to its detail page. If not, we save the new `Genre` and redirect to its detail page. Note that here we `await` on the result of the database query, following the same pattern as in other route handlers. @@ -148,7 +150,7 @@ if (genreExists) { } ``` -This same pattern is used in all our post controllers: we run validators (with sanitizers), then check for errors and either re-render the form with error information or save the data. We use the collation method when executing the query to retrieve the genre. This is done to ensure that the query is case-insensitive. By setting the locale to 'en' and strength to 2, we adjust the comparison sensitivity such that it ignores differences in case and diacritics. In practice, this means that if someone attempts to add a genre 'fantasy' with a lowercase 'f', it will be considered the same as 'Fantasy' with an uppercase 'F', and won't be treated as a distinct genre. +This same pattern is used in all our post controllers: we run validators (with sanitizers), then check for errors and either re-render the form with error information or save the data. ## View