1

I have a sails.js web application and I have a crawling function that get the title of a website then I want to return that value to the view.

I tried to assign the function to a variable and return it but always it says undefined.

const Crawler = require("crawler");

let c = new Crawler({
    maxConnections : 10,
    callback : function (error, res, done) {
        if(error){
            console.log(error);
        }else{
            let $ = res.$;
            let title = $("title").text();
        }
        return title
        done();
    }
});

module.exports = {
    testapi: function (req, res) {
        title = c.queue('http://www.agounichams.ml');
        sails.log(title);
        return res.send(c.title);
    }
}
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Chams Agouni
  • 364
  • 1
  • 12
  • 1
    Possible duplicate of [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) – dNitro Dec 30 '18 at 09:47

1 Answers1

2

Shouldn't it be title = await c.queue('http://www.agounichams.ml'); ?

And declaring title just as that, without a const, let, etc makes it global if that is intended.

Robertino Vasilescu
  • 1,038
  • 1
  • 7
  • 13