mdn / express-locallibrary-tutorial

Local Library website written in NodeJS/Express; example for the MDN server-side development NodeJS module: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

author controller ; author_create_post suggestion

PierreNoll opened this issue · comments

Hello,

First of all, thank you for this great tuto. I am still in it and I have a suggestion for the authorController.

I noticed that if one want to create a new author and fill all of the input correctly excepted one of the two dates then on submit both of the dates (the good one and the wrong one disappear).

This is because when you render the view in the author_create_post, that is the req.body that is passed to the view :

if (!errors.isEmpty()) {
            // There are errors. Render form again with sanitized values/errors messages.
            res.render('author_form', { title: 'Create Author', author: req.body, errors: errors.array() });
            return;
} 

I suggest that one should create the new author with the constructor and sanitized values before, and then pass this new author to the view as it will now contain the virtuals.

I might be wrong, plus I use EJS for the templates so maybe it may differ from the pug. If that is the case I am sorry to bother.

Thanks,

Hi @PierreNoll

Thanks for this. I just want to confirm what you think the symptoms/problem are here.

I think you are saying that the problem is that when you enter a bad date, the newly displayed form is redrawn without the bad date, but also without a good date. You think it should still display the good date. Is that correct?

Hi @hamishwillee

Thanks for the response.

Yes that's what I think. Though it is just a little detail, it may be confusing as we send back the sanitized values for these dates and they are not displayed in the newly form.

I think the author_update_post has the same problem.

If I enter a bad name and a good date, the good date won't be displayed in the newly form, then I have to enter the date again.

Would it be better to create a new author and pass it?

@Meteoors No, the author_update_post works correctly (reviewed and tested).

@PierreNoll Apologies for the delay getting back to you - I moved onto other tasks for a while. You have probably lost interest by now :-(

I suggest that one should create the new author with the constructor and sanitized values before, and then pass this new author to the view as it will now contain the virtuals.

Yes, this is what I did. The problem is that the sanitised format in req.body.dateXxxx is not the same as is expected in pug. However by putting the sanitised values into a new author object first, it gets correctly formatted for display. From your post, I see you understand this.

Thanks for your help - much appreciated.