2017-08-04 23 views
0

我剛開始使用Vorpal.js來幫助構建一個CLI工具,它將最終執行鍼對API端點的HTTP請求。在vorpal動作期間調用另一個函數

我有它提出問題和驗證數據輸入,但一旦我有了輸入,我有盧布調用另一個方法,從請求的答案收縮端點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工具簡單地存在。

我想這可能是範圍問題?

回答

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(); 
     }); 
    }) 
+0

facepalm時刻就在那裏,完全忘了這一點。原來我的變量是未定義的!謝謝。 – chinds

+0

好吧,它的工作原理! – dthree

相關問題