无法从node.js中的函数获取返回数据

时间:2018-12-05 08:45:57

标签: javascript node.js

我试图从下面由节点js编写的代码返回结果。 但是console.log显示“结果未定义。”。 我应该修改下面的代码?

function findRate(){
  request('https://finance.naver.com/marketindex/exchangeList.nhn', 
    (error, response, html) => {
      let result = [];

      if(!error && response.statusCode == 200) {
         const $ = cheerio.load(html);
         const exchange = $('.tit').next();

         for(let i = 0; i < 4; i++) {
            let a = $(exchange[i]).text();
            result.push(a);
         }
       }
   });
   return result;
 }

 var result1 = findRate();
 console.log(result1);

可以在其他文件中使用吗? 还是我需要使用其他模块,例如module.exports?

0 个答案:

没有答案