我想证明Joi.validate。
原始功能是:
Joi.validate(message, schema, (err, success) => {})
我试图做:
const Joi = require('joi');
const Promise = require('bluebird');
const validate = Promise.promisify(Joi.validate);
然后我打电话给
validate(message, schema)
.then(() => console.log('ok'))
.catch(err => console.log('ERRRR', err.message));
但是我收到此错误:
Cannot read property 'compile' of undefined
你知道我怎么能做到吗? 谢谢
答案 0 :(得分:0)
问题在于在承诺期间会丢失上下文。所以试试这个:
const validate = Promise.promisify(Joi.validate).bind(Joi);
validate(message, schema)
.then(() => console.log('ok'))
.catch(err => console.log('ERRRR', err.message));
或者这个:
const pJoi = Promise.promisifyAll(Joi);
pJoi
.validate(message, schema)
.then(() => console.log('ok'))
.catch(err => console.log('ERRRR', err.message));