-1

I am making a chat bot using Node.js. I use cheerio to scrape information from html and send it to users. I can scrape data and print it threw console but cannot return that information to a variable. Please help.

--Parsing part--

const cheerio = require("cheerio"),
      request = require("request");

const init_url = "https://stu.dje.go.kr/sts_sci_md01_001.do?schulCode=G100000208&schulCrseScCode=4&schulKndScCode=04&schMmealScCode=2";

const MealParser = {};

MealParser.parse = () => {
  console.log(''+init_url);
  return doRequest(makeURL(),function(err,data){
    console.log(data);
    return data;
  });
}

function doRequest(url,callback){
  request(url, function(err, resp, html) {
      var list = ["initial array"];
      if (!err){
      const $ = cheerio.load(html);
      $('.tbl_type3 th:contains("중식")').parent().children().each(function () {
        list.push($(this).text());
      });
      return callback(null,list);
    }
  });
}

function makeURL(){
  var result_url = init_url;
  return result_url;
}

module.exports = MealParser;

I use function "parse" to return the scrapped information to the main bot which looks like the code down below.

--using the parsed information--

var result = MealParser.parse();
지영채
  • 11
  • 4

1 Answers1

0

Your callbacks are returning a value, I think you should study on how callbacks work.

If you convert callback based functions to promises you can do the following:

MealParser.parse = () => {
  console.log(''+init_url);
  return doRequest(makeURL())
  .then(
    data=>{
      console.log(data);
      return data;
    },
    error=>console.error(error)
  )
}

function doRequest(url,callback){
  return new Promise(
    (resolve,reject)=>
      request(url, function(err, resp, html) {
        const list = ["initial array"];
        if(err){
          reject(err);
        }else{
          const $ = cheerio.load(html);
          $('.tbl_type3 th:contains("중식")').parent().children().each(function () {
            list.push($(this).text());
          });
          // return callback(null,list);//no use returning anything from a callback
          resolve(list);
        }
      })
  );
}
HMR
  • 37,593
  • 24
  • 91
  • 160