0
const request = require('request');
const cheerio = require('cheerio');

let url = 'https://website';

let test = request(url, function (err, res, body) {
    if (err && res.statusCode !== 200) throw err;

    let $ = cheerio.load(body);
    let title = $(".detail__title").text();
    let img = $('div.detail__media').find('img').attr('src');
    let date = $('div.detail__date').text();

    return title;
});
console.log(title);

the return always get undefined. How can i get data from the function? Return doesn't work.

lastr2d2
  • 3,604
  • 2
  • 22
  • 37
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CherryDT May 25 '21 at 06:34
  • 1
    Firstly, it's the `title` that is being printed using `console.log` at the end. If you need to access `title` outside it's scope (which is the function at present) you will have to change the scope of the variable from `let` to `var` while initialising it. If you need to access the value returned by `return title` that would be present in the variable `test` now. – Avani Khabiya May 25 '21 at 06:47

2 Answers2

0

Two ways you can achieve this:

First:

Using the callback. cb is a callback function here

let test = (cb) => { 
    request(url, function (err, res, body) {
       if (err && res.statusCode !== 200) cb(err, null);

       let $ = cheerio.load(body);
       let title = $(".detail__title").text();
       let img = $('div.detail__media').find('img').attr('src');
       let date = $('div.detail__date').text();

      cb(null, title);
   });}

then call test function like this:

test((err, title) => {
  if (err) { return console.log(err); }
  console.log(title);
});

Second:

Using the promises

const test = async () => {
  return new Promise((resolve, reject) => {
    request(url, function (err, res, body) {
        if (err && res.statusCode !== 200) return reject(err);
    
        let $ = cheerio.load(body);
        let title = $(".detail__title").text();
        let img = $('div.detail__media').find('img').attr('src');
        let date = $('div.detail__date').text();
    
        resolve(title);
    });
  });
};

then calling test function like this:

const title = await test().catch((err) => {
    console.log(err); // handle error
});
console.log(title);
Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52
  • in the first way, may i know why must using 'NULL' in cb function? – jodhykardo May 25 '21 at 08:41
  • @jodhykardo You can check more here https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/ and http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/#:~:text=There's%20really%20only%20two%20rules,for%20any%20successful%20response%20data.. It's error first pattern in a callback. – Surjeet Bhadauriya May 25 '21 at 10:57
0

You can get the result of callback function with promises.

const request = require('request');
const cheerio = require('cheerio');

const url = 'https://website';

const p =  new Promise((resolve, reject) => {
    request(url, function (err, res, body) {
        if (err && res.statusCode !== 200) reject(err);

        let $ = cheerio.load(body);
        let title = $(".detail__title").text();
        let img = $('div.detail__media').find('img').attr('src');
        let date = $('div.detail__date').text();
        resolve(title);
    });
});

p.then(title => {
    console.log(title);
});

// or you can get result with await

console.log(await p);
Yonghoon Lee
  • 2,217
  • 1
  • 12
  • 8