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 answer by Chris Visser doesn't apply here, as beriliox is trying to handle a request, not make one (which is what Meteor' HTTP package can help you with).
The solution to the problem is to declare the handler function
async. This is due to the seemingly synchronous nature of Meteor's server code running in fibers.