Storing in Global Variables

Asked by At

I'm using Papa Parse to parse a local CSV file.

When I print my updated objects to the console, I get the object to return with the changed values I have applied based on certain conditions.

My problem is getting the image dimensions: Width and Height.

Below is my script;

var w;
var h;
let testString = "s2345232";
if (/^[s]\d+$/.test(testString) == true) {
    url = baseUrl + testString + suffix;
    getMeta(url, function(width, height) {
        w = width;
        h = height; 
        console.log(w, h); //works
console.log(w, h); // doesn't work
// Here is the function to retrieve the image data
function getMeta(url, callback) {
    var img = new Image();
    img.src = url;
    img.addEventListener("load", function() {
        callback(this.naturalWidth, this.naturalHeight);

What am I doing wrong and how can I fix it?

2 Answers

Jaybird On

There's a problem with the way you generate the header rows for your new columns. Papa expects each row to have the same column names, but by specifying newHeader1 = propString + "_Size" etc, it means the column names will be different. Could you just use a constant for each column name? E.g. newHeader1 = "Size".

Community On

Just using newHeader1 = 'Size'