0

I am having trouble getting connect flash to work in the sequelize callback function.

router.route('/')
    .post(function(aRequest, aResponse) {
        var data = aRequest.body;

        models.users.findOne({
            where: {
                email: data.email
            }
        }).then(function (aUser) {
            if (!aUser) {
                bcrypt.hash(data.password, null, null, function(err, hash) {
                    if(err) {
                        return next(err);
                    }
                    models.users.create({
                            firstname       : data.firstname,
                            lastname        : data.lastname,
                            email               : data.email,
                            password        : hash
                    }).then(function () {
                        aRequest.flash('success', 'User successfully created.');
                    }).done(function () {
                        aResponse.redirect('/login');
                    });
                });
            }
            else {
                // aRequest.flash('error', 'This email address is already registered');
                aResponse.redirect('/login');
            }
        });
});

Above is my current code, have tried a few variations on it, calling both flash and redirect in the .then(), tried 2 .then()'s and now the .done().

I am getting the following error:

Unhandled rejection TypeError: undefined is not a function at Instance.set (/home/node/shared/Heroku/landbou/node_modules/sequelize/lib/instance.js:348:68)

Which is easily resolved by removing aRequest.flash(...).

And yes, 'router.use(flash());' is being called higher up.

Everything continues so its not an app breaking error, but I do need the messages to flash up, otherwise I have to create additional wasted routes to handle the success/fail for user registrations.

RemeJuan
  • 823
  • 9
  • 25
  • Start by doing a .catch(next) at the end of each promise chain. That should hopefully give you more information. – grimurd Oct 28 '15 at 11:55
  • I cleaned your example up a little bit to handle errors better so we can see what is going on. [Link](http://pastebin.com/cjjmMx9a) – grimurd Oct 28 '15 at 12:04

0 Answers0