1

I'm trying to get my promise chain to work in my express post, but can't seem to figure out why it won't work, even though I've done this before.

I added many logs to follow where it stops, and it seems to stop after the validation resolve, does not continue

Promise:

    router.post('/auth/token', function (req, res) {
      var schema = require('./schema/token.js');

      var data = {
        username: req.body.username,
        password: req.body.password
      };

      new Promise(function (resolve, reject) {
        logger.info(`Validating request..`);
        return validator.validate(schema, data);
      }).then(function () {
        logger.info(`Getting token..`);
        return authentication.getToken(data.username, data.password);
      }).then(function (result) {
        logger.info(`Received token..`);
        res.send(result);
      }).catch(function (err) {
        logger.info(`Unable to receive token..`);
        res.send(err);
      })
  })

Validator.js:

module.exports.validate = function (schema, data) {
    return new Promise(function(resolve, reject){
        logger.info(`Loading schema..`);
        if (!schema) {
            logger.info(`Missing schema, rejecting promise..`);
            return reject(new Error('Missing schema'));
        }

        const ajv = new Ajv({ v5: true, allErrors: true });

        logger.info(`Compling schema..`);
        const validate = ajv.compile(schema);
        logger.info(`Validating schema..`);
        const valid = validate(data);

        if (!valid) {
            const errors = validate.errors.map((error) => `${error.dataPath.replace('.', '')}: ${error.message}`);
            const err = new Error(errors);
            return reject(err);
        }

        logger.info(`Valid schema.. resolving..`);
        return resolve();
    })
}

When I run this.. the logs say the following:

info: Validating request..
info: Loading schema..
info: Compling schema..
info: Validating schema..
info: Valid schema.. resolving..

No longer continues, it should continue to the next promise, now if I change the first promise and force a resolve and reject, it'll work but as far as I know, that should not be required as the validate returns a promise and I'm not getting any errors

Any ideas?

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
jaekie
  • 2,283
  • 4
  • 30
  • 52

1 Answers1

1

Don't create a new promise, use the one from validate; see *** below:

router.post('/auth/token', function (req, res) {
    var schema = require('./schema/token.js');

    var data = {
      username: req.body.username,
      password: req.body.password
    };

    logger.info(`Validating request..`);      // ***
    validator.validate(schema, data)          // ***
    .then(function () {
      logger.info(`Getting token..`);
      return authentication.getToken(data.username, data.password);
    }).then(function (result) {
      logger.info(`Received token..`);
      res.send(result);
    }).catch(function (err) {
      logger.info(`Unable to receive token..`);
      res.send(err);
    })
})

The issue is you never resolve the new promise you create. But since there's no good reason to create a new promise when you already have one, the solution is to use the one you have.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875