0

New to nodejs and really programming in general except some basic PHP. I think my problem is more basic javascript than anything though.

How can I access variables 'latitude' and 'longitude' outside the object 'img'?

console.log(latitude + '|' + longitude) is displaying the data I want but in console.log(img) all img properties are empty including exifData.gps.GPSLatitude & exifData.gps.GPSLongitude.

var ExifImage = require('exif').ExifImage;
var img_loc = 'c:/node/test/wherepic/uploaded_imgs/img.JPG';

var img = new ExifImage({ image : img_loc }, function (error, exifData) {
        var latitude = exifData.gps.GPSLatitude;
        var longitude = exifData.gps.GPSLongitude;
        console.log(latitude + '|' + longitude);
});

console.log(img);
  • 1
    Thats because the `console.log` is executed before the data is available. Keyword: asynchronous – RienNeVaPlu͢s Oct 13 '14 at 21:39
  • You will need to understand how asynchronous responses work in javascript. The answer that your question is marked a duplicate of contains a lot of helpful explanation about dealing with asynchronous repsonses. It's about an Ajax call, but the exact same info applies to your situation. – jfriend00 Oct 13 '14 at 21:48

1 Answers1

0

The problem is not the object, but the fact that your second console.log is executed before the data becomes available. Only when the function is called that data becomes available. You must access it inside the function.

Tim Jansen
  • 3,330
  • 2
  • 23
  • 28