我刚刚开始使用Vorpal.js来帮助构建一个CLI工具,该工具最终将针对一个API端点执行一个HTTP请求。
我让它提出问题并验证数据输入,但一旦我有了输入,我就让rouble调用另一个方法,该方法从请求的答案中收缩端点url。
下面是我的代码:
function apiEndpoint (env) {
if (env === 'INT') {
return API_ENDPOINT_INT;
} else if (env === 'TEST') {
return API_ENDPOINT_TEST;
} else if (env === 'LIVE') {
return API_ENDPOINT_LIVE
}
}
function constructRequestURL (params) {
let API_ENDPOINT = apiEndpoint(params.env);
let requestURL = `${API_GATEWAY}${API_ENDPOINT}?asset_uri=${params.asset_uri}&${site_name}&${asset_type}&${event_type}`
return requestURL;
}
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
})
})
vorpal
.delimiter('AMP ReRenderer$')
.show();下面的注释行是有效的
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);无论如何,当调用constructUrl函数时,什么都没有发生,cli工具只是存在。
我猜这可能是范围问题?
发布于 2017-08-05 00:23:29
不是的。它可能正在悄悄地失败。始终确保你的承诺有一个catch。
vorpal
.command('render')
.option('--dave')
.action(function (args, cb) {
const self = this;
this.prompt(questions)
.then (function (result) {
// self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
let requestURL = constructRequestURL(result);
self.log(`Request URL: ${requestURL}`);
cb();
}).catch(err => {
self.log(err);
cb();
});
})https://stackoverflow.com/questions/45510358
复制相似问题