【问题标题】:calling another function during vorpal action在 vorpal 动作期间调用另一个函数
【发布时间】:2017-08-05 00:23:28
【问题描述】:

我刚刚开始使用 Vorpal.js 来帮助构建一个 CLI 工具,该工具最终将针对 API 端点执行 HTTP 请求。

我让它提出问题并验证数据输入,但是一旦我有输入,我就会有卢布调用另一种方法,该方法会根据请求的答案收缩端点 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 工具只是存在。

我猜可能是范围问题?

【问题讨论】:

    标签: javascript node.js vorpal.js


    【解决方案1】:

    没有。它可能是默默地失败了。始终确保您的承诺上有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();
          });
      })
    

    【讨论】:

    • 面对面的时刻就在那里,完全忘记了这一点。原来我一个变量是未定义的!谢谢。
    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多