Adding Flash Messages to more Actiions

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 341:- Adding Flash Messages to more Actiions

To add flash messages to more actions, you will need to follow these steps:

  1. Install the connect-flash package using npm:

arduinoCopy code

npm install connect-flash

  1. Require the package in your app.js file:

javascriptCopy code

const flash = require('connect-flash');

  1. Add the flash middleware to your app:

javascriptCopy code

app.use(flash());

  1. Create a flash message in your controller, just like before:

javascriptCopy code

req.flash('success', 'Post deleted successfully');

  1. In the controller action where you want to display the message, retrieve the message from the flash and pass it to the view:

javascriptCopy code

const flashMessages = res.locals.messages(); res.render('index', { flashMessages });

  1. In your view, add code to display the flash message:

ejsCopy code

<% if (flashMessages.success) { %>  <div class="alert alert-success" role="alert">    <%= flashMessages.success %>  </div> <% } %>

  1. Repeat steps 4-6 for any other actions where you want to display flash messages.
  2. You can also use the error message type for displaying error messages:

javascriptCopy code

req.flash('error', 'There was an error deleting the post');

And in your view:

ejsCopy code

<% if (flashMessages.error) { %>  <div class="alert alert-danger" role="alert">    <%= flashMessages.error %>  </div> <% } %>

By adding flash messages to more actions, you can provide better feedback to the user and improve the overall user experience.

39. Async Await + Error Handling

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?