1

I'm trying to use this method for non-sequential promise output.

The express .json call successfully sends a 201 alongside the user object from the API, but in the console, I get the Unhandled rejection error shown below. This seems like it should be caught by the .catch handler in the controller. I am wondering why this could be happening?

userController

module.exports.postUser = function (req, res, next) {

  var user = req.body.user;
  var ip = req.ip;

  userService.createUser(user, ip)
    .then(function (user) {
      res.status(201).json({"user": user.toJSON()});
    })
    .catch(function (err) {
      return next(err);
    });
};

userService

module.exports.createUser = function (user, ip) {
  var user = new Promise(function (resolve, reject) {
    return resolve(User.forge(user));
  });

  return user.then(function validateUser(user) {
    return user.validate({validatePassword: true});
  })
    .then(function hash() {
      var password = user.value().get('password');
      return hashPassword(password);
    })
    .then(function setPassword(hashedPass) {
      user.value().set('hashedPass', hashedPass);
      return user.value().save();
    })
    .then(function () {
      return user;
    });
};

output

Unhandled rejection error: null value in column "status" violates not-null constraint
    at Connection.parseE (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:105:22)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)
From previous event:
    at Client._query (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/dialects/postgres/index.js:122:12)
    at Client.query (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/client.js:127:24)
    at Runner.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:118:24)
From previous event:
    at /Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:44:21
From previous event:
    at Runner.run (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:30:20)
    at QueryBuilder.Target.then (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/interface.js:27:43)
    at null.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/bookshelf/lib/model.js:208:36)
    at processImmediate [as _immediateCallback] (timers.js:367:17)
mrbeskin
  • 25
  • 4
  • Ah, right, I didn't notice that `user` is the promise and `.value()` is the synchronous inspection. Btw, there are [other ways](http://stackoverflow.com/q/28250680/1048572) to solve this problem, maybe better than synchronous inspection. – Bergi Jul 29 '15 at 13:37
  • Great! Thank you for the link. Very new to promises so the suggestion is appreciated. – mrbeskin Jul 29 '15 at 16:38

1 Answers1

2

The most likely cause is that return user.value().save() does not return a promise, but implements callbacks instead. If that is the case, then the error would be thrown outside of the native promise try/catch block, and would hence not be caught but your .catch().

Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33
  • Ah - so it wasn't returning a callback, but it wasn't returning a promise either! So this was right. We applied the save method, but didn't return the promise from it so it did turn out to be the lack of a promise breaking in the controller. Thank you for the suggestion! – mrbeskin Jul 28 '15 at 21:18