Tuesday 23 May 2017

Nodejs - Promise code not executing

I'm not sure what the problem is... it may have to do with my promise handling. This is a nodejs/express app. I'm using Sequelize.js to interact with my DB, and it uses promises.

Essentially, a user can make the post request below.

Now, condition1 works fine. condition3 also works fine. However, if the result is condition2 the console.log message Condition 2 success! will execute, but the res.send('success2!'); code will not execute... the code will just hang there.

router.post('/checkUserRoute', function(req, res, next) {

    const username = req.body.username;
    if (condition1) {
        userTable.findOne({
            where: {
                username: username
            }
        }).then(function(user) {
            if (condition2) {
                console.log('Condition 2 success!')
                res.send('success2!');
            }
            if (condition3) {
                user.update({
                    username: 'NewUserName' 
                }).then(function() {
                    console.log('Condition 3 success!');
                    res.send('success3!');
                });
            }
        }).catch(function(err){
            res.send('error');
        });
    }
});

When condition2 is met and it gets to the line res.send('success2!'); the following message is displayed in my terminal:

Warning: a promise was created in a handler at anonymous> ... but was not returned from it, see and then it displays the following link:

http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it

The link recommends adding return, but that hasn't helped.



via MonkeyOnARock

No comments:

Post a Comment