Hello everyone i've a callback function in one route with Iron Router in my meteor project.
The issue is when i run the path localhost:3000/scraper, the console shows the below message: Error: Can't wait without a fiber
This code scrape to one page 'x'.
Router.route('/scraper', function(){
this.response.setHeader( 'Access-Control-Allow-Origin', '*' );
this.response.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE' );
this.response.setHeader( 'Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, x-request-metadata' );
this.response.setHeader( 'Access-Control-Allow-Credentials', true );
var url = 'https://scholar.google.cl/citations?user= ... &hl=es';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var json_pre = { id_usuario: "",
autor : "",
imagen_usuario: ""
};
$('#gsc_prf_in').filter(function(){
var data = $(this);
autor = data.text().trim();
json_pre.autor = autor;
})
$('input[name="user"]').filter(function(){
var data = $(this);
id_usuario = data.val();
json_pre.id_usuario = id_usuario;
})
$('#gsc_prf_pup').filter(function(){
var data = $(this);
imagen_usuario = data.attr('src');
json_pre.imagen_usuario = imagen_usuario;
})
.....
}
var json = JSON.stringify(json_pre, null, 4);
//************************************************
//Can't wait without a fiber
var id_usuario_m = Scrapers.findOne({id_usuario :json_pre.id_usuario });
if (id_usuario_m) {
Scrapers.update({id_usuario :json_pre.id_usuario }, {$set: json_pre});
console.log('Usuario Actualizado');
} else {
Scrapers.insert(json_pre);
console.log('Usuario Insertado')
}
//************************************************
})
this.response.end('Fin de la scrapeada');
}, {where : "server"});
If someone could to help me, I would be be so grateful. Thanks so much to all.
The request library is a node library that only works on the server. Running node libraries in meteor that require async callbacks like require does requires a little bit of extra touch
Meteor runs everything in a so called Fiber. An equivalent to a javascript promise. This makes all serverside code in Meteor context by default synchronous which makes code easier to read. However nodejs don't use Fibers and therefore you will need to wrap their callback functions like this:
However since you are using Meteor. Why not use its HTTP library? It works similar to node's request library, but also for the clientside and its already available for you!