0

I'm using express app.param to load objects from db (app.param('userId', users.userById);):

exports.userById = function (req, res, next, id) {
    return user.findOne({
        where: { id: id }
    }).then(function (result) {
        req.user = result;
        next();
    }).catch(function (error) {
        next(error);
    });
};

After that I update the loaded object with the following code.

exports.update = function (req, res) {
    var user = req.user;
    return user.update({
        //update properties
    }).then(function () {
        res.end();
    }).catch(function (error) {
        //error handling
    });
};

For some reason I get the warning that "a promise was created in a handler but was not returned from it".

I can't see why, but that always happen when I use a routing parameter that uses sequelize before making the actual changes to the database.

What is the correct way to do this?

I'm using sequelize v3.23.3 with Postgres.

EDIT

I changed the code to a more simple example that throws the same warning.

belyid
  • 841
  • 2
  • 12
  • 28

1 Answers1

1

If you forget to return that request promise, the next handler executes immediately with an argument of undefined - which is completely valid for the Promises/A+ spec, but I don't think that it is what you are looking for.

See How to execute code after loop completes for solutions.

Community
  • 1
  • 1
AlexB
  • 3,518
  • 4
  • 29
  • 46
  • I only call next after sequelize return a value from the database. Or am I missing something? – belyid Jul 08 '16 at 09:38