2

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.

MonkeyOnARock
  • 2,151
  • 7
  • 29
  • 53
  • Can you try to add return statement before `...res.send('success2!');` and `...user.update({` and also before `...res.send('success3!');` – muratgozel May 23 '17 at 21:50
  • I added the three `return` statements... and like before, `condition1` and `condition3` work fine, but it still hangs in the same spot at `condition2`. The only difference this has made is that now, in the terminal, the message `Warning: a promise was created in a handler...` no longer appears. – MonkeyOnARock May 23 '17 at 21:53
  • That's really weird, neither `console.log` nor `res.send` are know to hang; and their execution is synchronous. Are you doing anything uncommon to your environment? – Bergi May 23 '17 at 21:56
  • It is really weird! I've integrated `promises` and `if` statements like this before, and never have I had this problem... in the same route file, I have other routes that have `promises` and `if` statements just like this, and they don't have any problems. And to be clear: the `console.log` doesn't hang... only the `res.send` in `condition2` hangs. – MonkeyOnARock May 23 '17 at 22:00
  • Missing `;` following `console.log()` call? – guest271314 May 23 '17 at 22:03
  • 1
    @guest271314 [No](https://stackoverflow.com/q/2846283/1048572) – Bergi May 23 '17 at 22:17
  • @MonkeyOnARock How exactly do you experience the "hanging"? Is there code afterwards that you expect to be executed (`condition3`?) and what kind of debugging do you use to know that it doesn't? Or is it just about the client never receiving the response? – Bergi May 23 '17 at 22:19
  • @Bergi the client never receives the response. In terms of debugging... I use `morgan`, so if the route is successful it will display a `200` success code in the terminal. But for this route, when it gets to `condition2`, after displaying the `console.log` it will hang at `res.send`, and in the terminal it eventually gives a `500` error code. – MonkeyOnARock May 23 '17 at 22:34
  • 1
    In `Express` defining a route with `function (req, res, next) { ... }` means that you have to call `next()` when you are done. Either call `next()` before each return or, since you complete the request here, just remove the declaration. ie. `function (req, res) { ... }`. Common `Express` error. – Douglas Parker May 23 '17 at 23:48

0 Answers0