Wednesday, 31 May 2017

Get data out of callbacks

I am using "pdf-text" module for Node.js to convert a pdf into a string array and then get specific elements out of it. But the problem is, I can only access the data, "chunks", only when I am inside the callback. I want to store it in some global variable so that I can use it in different files. I have tried storing the elements of the array inside variables while inside the function, but no luck. Here's the code:

var pdfText = require('pdf-text');

var pathToPdf = "PDF FILE NAME";

var fs = require('fs');
var buffer = fs.readFileSync(pathToPdf);

var output;

pdfText(buffer, function(err, chunks){

    if (err){
      console.dir(err);
      return;
    }
    console.dir(chunks);
    output = chunks;
}

console.dir(output);

P.S. I am fairly new to Node.js and JavaScript and help would be appreciated greatly.

Thanks in-advance!



via betelguese123

No comments:

Post a Comment