req.flash not working with the res.redirect












0














I am somewhat new to the node and express world so please forgive any of my nobe mistakes, I have a couple of methods that are returning me 500 after I use something like this.



refrence to the error



req.flash('success', 'your password has been successfully changed.');
res.redirect('/user-profile');


also I have tried sending status with it like



res.redirect(302,'/user-profile');


but it also didn't worked out



and when i try to reload the page it works fine (on the second attempt), however if I comment req.flash every things starts to work out just fine, seems like it's causing the issue while it works fine with other methods where I am using it like



req.flash('success', 'You are logged out');
res.redirect('/login');


Code in my index.js file



const express = require('express');
const router = express.Router();
const fs = require('fs');
const bcrypt = require('bcryptjs');
const app = require('../app');
const User = require('../model/user');
const passport = require('passport');
let path = require('path');
router.get('/user-profile', (req,res,next) => {
res.render('profile',{
title: 'Profile'
});
});

router.get('/logout', (req,res,next) => {
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/login');
});


Code in my app.js



// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});


I have tried to find the solution online, but could not get it working. I am really finding it hard to debug any help would be greatly appreciated.










share|improve this question
























  • If you're getting 500 errors, there should be errors being logged by the server.
    – robertklep
    Nov 20 '18 at 11:33










  • @robertklep where could those be??? where should i be finding them??
    – DaShInG Sid
    Nov 20 '18 at 11:35










  • How do you start the server?
    – robertklep
    Nov 20 '18 at 11:36










  • i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
    – DaShInG Sid
    Nov 20 '18 at 11:37








  • 1




    The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
    – robertklep
    Nov 20 '18 at 11:48


















0














I am somewhat new to the node and express world so please forgive any of my nobe mistakes, I have a couple of methods that are returning me 500 after I use something like this.



refrence to the error



req.flash('success', 'your password has been successfully changed.');
res.redirect('/user-profile');


also I have tried sending status with it like



res.redirect(302,'/user-profile');


but it also didn't worked out



and when i try to reload the page it works fine (on the second attempt), however if I comment req.flash every things starts to work out just fine, seems like it's causing the issue while it works fine with other methods where I am using it like



req.flash('success', 'You are logged out');
res.redirect('/login');


Code in my index.js file



const express = require('express');
const router = express.Router();
const fs = require('fs');
const bcrypt = require('bcryptjs');
const app = require('../app');
const User = require('../model/user');
const passport = require('passport');
let path = require('path');
router.get('/user-profile', (req,res,next) => {
res.render('profile',{
title: 'Profile'
});
});

router.get('/logout', (req,res,next) => {
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/login');
});


Code in my app.js



// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});


I have tried to find the solution online, but could not get it working. I am really finding it hard to debug any help would be greatly appreciated.










share|improve this question
























  • If you're getting 500 errors, there should be errors being logged by the server.
    – robertklep
    Nov 20 '18 at 11:33










  • @robertklep where could those be??? where should i be finding them??
    – DaShInG Sid
    Nov 20 '18 at 11:35










  • How do you start the server?
    – robertklep
    Nov 20 '18 at 11:36










  • i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
    – DaShInG Sid
    Nov 20 '18 at 11:37








  • 1




    The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
    – robertklep
    Nov 20 '18 at 11:48
















0












0








0







I am somewhat new to the node and express world so please forgive any of my nobe mistakes, I have a couple of methods that are returning me 500 after I use something like this.



refrence to the error



req.flash('success', 'your password has been successfully changed.');
res.redirect('/user-profile');


also I have tried sending status with it like



res.redirect(302,'/user-profile');


but it also didn't worked out



and when i try to reload the page it works fine (on the second attempt), however if I comment req.flash every things starts to work out just fine, seems like it's causing the issue while it works fine with other methods where I am using it like



req.flash('success', 'You are logged out');
res.redirect('/login');


Code in my index.js file



const express = require('express');
const router = express.Router();
const fs = require('fs');
const bcrypt = require('bcryptjs');
const app = require('../app');
const User = require('../model/user');
const passport = require('passport');
let path = require('path');
router.get('/user-profile', (req,res,next) => {
res.render('profile',{
title: 'Profile'
});
});

router.get('/logout', (req,res,next) => {
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/login');
});


Code in my app.js



// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});


I have tried to find the solution online, but could not get it working. I am really finding it hard to debug any help would be greatly appreciated.










share|improve this question















I am somewhat new to the node and express world so please forgive any of my nobe mistakes, I have a couple of methods that are returning me 500 after I use something like this.



refrence to the error



req.flash('success', 'your password has been successfully changed.');
res.redirect('/user-profile');


also I have tried sending status with it like



res.redirect(302,'/user-profile');


but it also didn't worked out



and when i try to reload the page it works fine (on the second attempt), however if I comment req.flash every things starts to work out just fine, seems like it's causing the issue while it works fine with other methods where I am using it like



req.flash('success', 'You are logged out');
res.redirect('/login');


Code in my index.js file



const express = require('express');
const router = express.Router();
const fs = require('fs');
const bcrypt = require('bcryptjs');
const app = require('../app');
const User = require('../model/user');
const passport = require('passport');
let path = require('path');
router.get('/user-profile', (req,res,next) => {
res.render('profile',{
title: 'Profile'
});
});

router.get('/logout', (req,res,next) => {
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/login');
});


Code in my app.js



// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});


I have tried to find the solution online, but could not get it working. I am really finding it hard to debug any help would be greatly appreciated.







node.js express redirect flash-message connect-flash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 12:31

























asked Nov 20 '18 at 11:19









DaShInG Sid

5319




5319












  • If you're getting 500 errors, there should be errors being logged by the server.
    – robertklep
    Nov 20 '18 at 11:33










  • @robertklep where could those be??? where should i be finding them??
    – DaShInG Sid
    Nov 20 '18 at 11:35










  • How do you start the server?
    – robertklep
    Nov 20 '18 at 11:36










  • i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
    – DaShInG Sid
    Nov 20 '18 at 11:37








  • 1




    The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
    – robertklep
    Nov 20 '18 at 11:48




















  • If you're getting 500 errors, there should be errors being logged by the server.
    – robertklep
    Nov 20 '18 at 11:33










  • @robertklep where could those be??? where should i be finding them??
    – DaShInG Sid
    Nov 20 '18 at 11:35










  • How do you start the server?
    – robertklep
    Nov 20 '18 at 11:36










  • i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
    – DaShInG Sid
    Nov 20 '18 at 11:37








  • 1




    The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
    – robertklep
    Nov 20 '18 at 11:48


















If you're getting 500 errors, there should be errors being logged by the server.
– robertklep
Nov 20 '18 at 11:33




If you're getting 500 errors, there should be errors being logged by the server.
– robertklep
Nov 20 '18 at 11:33












@robertklep where could those be??? where should i be finding them??
– DaShInG Sid
Nov 20 '18 at 11:35




@robertklep where could those be??? where should i be finding them??
– DaShInG Sid
Nov 20 '18 at 11:35












How do you start the server?
– robertklep
Nov 20 '18 at 11:36




How do you start the server?
– robertklep
Nov 20 '18 at 11:36












i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
– DaShInG Sid
Nov 20 '18 at 11:37






i start it using nodemon, and there's only 500 error for get /profile route in the terminal, nothing more
– DaShInG Sid
Nov 20 '18 at 11:37






1




1




The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
– robertklep
Nov 20 '18 at 11:48






The picture isn't very helpful. It looks like you're running your code in "production" mode, which also isn't very helpful because it obfuscates any errors that are occurring. Try adding a custom error handler (as explained here) that logs the error. Also, run your script in development mode (on Unix: NODE_ENV=development node yourserver.js). Also, for connect-flash to work properly, you need to have sessions enabled. Is that the case?
– robertklep
Nov 20 '18 at 11:48














1 Answer
1






active

oldest

votes


















0














Solved my error long time ago, in case anyone else is facing the same problem.Here is the answer to it. I had a duplicate of <%- messages() %> in my single ejs file which was causing the issue which eventually was crashing the app.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391864%2freq-flash-not-working-with-the-res-redirect%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Solved my error long time ago, in case anyone else is facing the same problem.Here is the answer to it. I had a duplicate of <%- messages() %> in my single ejs file which was causing the issue which eventually was crashing the app.






    share|improve this answer


























      0














      Solved my error long time ago, in case anyone else is facing the same problem.Here is the answer to it. I had a duplicate of <%- messages() %> in my single ejs file which was causing the issue which eventually was crashing the app.






      share|improve this answer
























        0












        0








        0






        Solved my error long time ago, in case anyone else is facing the same problem.Here is the answer to it. I had a duplicate of <%- messages() %> in my single ejs file which was causing the issue which eventually was crashing the app.






        share|improve this answer












        Solved my error long time ago, in case anyone else is facing the same problem.Here is the answer to it. I had a duplicate of <%- messages() %> in my single ejs file which was causing the issue which eventually was crashing the app.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 5:32









        DaShInG Sid

        5319




        5319






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391864%2freq-flash-not-working-with-the-res-redirect%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy