我有一個關於denodeify和承諾的問題。我有一些代碼(如下所示);我使用的是[email protected],我只想對Joi.validate進行修改。如果我做類似無極denodeify Joi.validate
var jv = Promise.denodeify(Joi.validate);
return jv(Qs.parse(request.query), joiSchema).then(function(value) {
console.log("value = " + JSON.stringify(value));
return reply("OK").code(200);
});
而我給它有效的查詢字符串參數(見下面的代碼),它按預期工作。如果我給出30的rowCount,但是,它只是掛起。我不能在我的then()中使用函數(err,value),那麼如何在承諾鏈中處理Joi驗證錯誤/消息?這可能嗎?
這裏有沒有denodeify工作代碼:
(function() {
var Hapi = require("hapi");
var Boom = require("boom");
var Qs = require("qs");
var Joi = require("joi");
var Promise = require("promise");
var hostName = "SEMrush";
var hostPort = 4355;
// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
host: "localhost",
port: hostPort
});
server.route({
method: "GET",
path: "/joiTest",
handler: function(request, reply) {
var joiSchema = Joi.object().keys({
phrase: Joi.string().alphanum().required(),
rowCount: Joi.number().integer().min(1).max(20)
});
Joi.validate(Qs.parse(request.query), joiSchema, function(err, value) {
if (err) {
console.log("err = " + err);
return reply(Boom.badRequest(err.message));
} else {
console.log("value = " + value);
return reply("OK").code(200);
}
});
}
});
// Default catch-all route.
server.route({
method: '*',
path: '/{p*}',
handler: function(request, reply) {
return reply("The requested resource was not found.").code(404);
}
});
server.start(function() {
console.log("\r\nService started on port " + hostPort);
});
})();
你的意思是30根引線的錯誤(承諾被拒絕rowCount時,你的回調從來不叫,應用程序掛起...),或「denodeify」不起作用? – Bergi