2016-09-23 12 views
-2

私は完全に機能している前提の機能連鎖を持っています。しかし、私はまだこのすべてが正確にどのように機能するかを理解していないと私がデータにアクセスできる唯一の方法は、このようなものです:私が代わりにconsole.logの引数を渡すためにしようとしているときにpromise chain functionからmodule.exportへの返信返信

getAxiosUrls() 
    .then(getNbShares) 
    .then(console.log) 

としてエラーが発生しましたargument is not defined。だから、どのように私はこのようなもので、私の(console.log)応答を渡します:

const my_data = function(req, res) { 
    //probably something else here 
return res.send(response); 
} 

私はので、私はmodule.exportsを行う必要が終わりに急行して働いている:

再び
module.exports = { 
    my_data 
}; 

私があればすみませんこの音はすべて混乱します。私はまだ学んでいますが、約束、コールバック、リクエスト方法では非常に混乱しています。ここで必要に応じて


は私の約束の関数である。

まず:

const getAxiosUrls = function() { 
    return axios.get("http://localhost:3000/gatable") 
     .then(function (response) { 
      return urls = response.data.rows.map(([x, y, z]) => y) 
    }) 
} 

第二:

const getNbShares = function() { 
    return Promise.map(urls, requestPromise) 
     .map((htmlOnePage, index) => { 
      const $ = cheerio.load(htmlOnePage); 
      const share = $('.nb-shares').html(); 
      let shareTuple = {}; 
       shareTuple[urls[index]] = share; 
       return shareTuple; 
     }) 
     .catch((e) => console.log('We encountered an error' + e)); 
} 

、その後:

getAxiosUrls() 
    .then(getNbShares) 
    .then(console.log) 
+0

この問題について多くの質問を投稿する前に約束に関するいくつかの基本をお読みください。このサイトのドキュメントセクションには、約束についての優れた記事があります:https://stackoverflow.com/documentation/javascript/231/promises – forrert

答えて

0

は、[OK]をので、私は最終的にこの思い付く:

const shares = function(req, res) { 
    return getAxiosUrls() 
    .then(getNbShares) 
    .then(function (response, error) { 
    return res.send(response);; 
});}