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

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

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

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support