I'm trying to scrape data from a word document with node.js.
My current problem is that the below console log will return the value inside the juice block as the appropriate varaible. If I move that to outside the juice block it is completely lost. I tried putting return
function getMargin(id, content){
var newMargin = content.css("margin-left");
if(newMargin === undefined){
var htmlOfTarget = content.toString(),
whereToCut = theRaw.indexOf("<div class=WordSection1>");
fs.writeFile("bin/temp/temp_" + id + ".htm", theRaw.slice(0, whereToCut) + htmlOfTarget + "</body> </html>", function (err){
if (err) {
throw err;
}
});
juice("bin/temp/temp_" + id + ".htm", function (err, html) {
if (err) {
throw err;
}
var innerLoad = cheerio.load(html);
newMargin = innerLoad("p").css("margin-left");
console.log(newMargin); // THIS newMargin AS VALUE
});
}
console.log(newMargin);//THIS RETURNS newMargin UNDEFINED
return newMargin;
}
I think the problem lies with fs.write and juice being Asyc functions. I just have no idea how to get around it. I have to be able to call getMargin at certain points, in a sequential order.