Anand Prem
Anand Prem

Reputation: 71

EXIF-JS data is getting undefined outside of the method

I am developing a web application using Angular 8. I just want to access the exif data of an input image outside the getData method by assigning the obtained data to a global variable. If i tried to access the global variable after executing function it is showing undefined. here is the function for extracting the exif data:

photodata: any; // global variable
getPhotodata(file) {
    EXIF.getData(file, function() {
      const data = EXIF.getAllTags(this);
      console.log(data); // working
      console.log(data.Make); // working
      console.log(data.Model); // working
      console.log(data.DateTimeOriginal); // working
      this.photodata = data;
      console.log(this.photodata) // working
  });
  }
  console.log(this.photodata) // here it is showing undefined

i have tried to return the data. But it also does not worked

getPhotodata(file) {
    EXIF.getData(file, function() {
      const data = EXIF.getAllTags(this);
      console.log(data); // working
      console.log(data.Make); // working
      console.log(data.Model); // working
      console.log(data.DateTimeOriginal); // working
      return data;
  });
  }
console.log(getPhotodata(file)) // undefined

Upvotes: 1

Views: 1295

Answers (1)

Mike Kovařík
Mike Kovařík

Reputation: 312

I'm aware you probably solved this by now, but in case async programming got you curious, you might enjoy my library exifr. I've written it because exif-js is effectively dead now (not maintained for over 2 years), it's pretty ineffective. Exifr is built around promises and async/await syntax, so you can do this:

async function getExif() {
  let output = await exifr.parse(file)
  console.log(data.Make)
  console.log(data.Model)
}

You can also try out the library's playground and experiment with images and their output, or check out the repository and docs.

Upvotes: 2

Related Questions