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