1
我试图让我的承诺链在我的快递中工作,但似乎无法弄清楚为什么它不起作用,即使我之前完成了此操作。承诺链似乎不会继续
我增加了许多日志遵循它停止,它似乎验证决心后停止,不会继续
承诺:
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();
})
}
当我运行此..日志说下列:
info: Validating request..
info: Loading schema..
info: Compling schema..
info: Validating schema..
info: Valid schema.. resolving..
不再继续,它应该继续下一个承诺,现在如果我更改第一个承诺并强制决定并拒绝,它就会工作,但据我所知,应该不需要,因为验证返回一个承诺,我没有得到任何错误
任何想法?
Ha..duh!谢谢..我知道我错过了一些愚蠢的东西!..将在一分钟内回答 – jaekie