javascriptexif-js

Change Undefined to Empty String


I'm using exif.js to pull metadata from images I upload to a CMS. When images do not have meta data, some values return "undefined". What I would like to do is change this "undefined" value to an empty string using something like this: (Credit: Stanley below)

   // not set var i means
// i === undefined

var j = (typeof (i) !== 'undefined' ? i : 'not found');

console.log(j); // 'not found'

Yet, After a few hours looking at the code, I still cannot figure out how exif.js is returning the undefined value so that I can change it to an empty string.

Link to exif.js: https://github.com/exif-js/exif-js/blob/master/exif.js


Solution

  • Here is the solution that worked for me:

    You can simply use ifstatements in the .getData function around your variables to not allow the undefined to run in the EXIF.JS library. Note this code is usually written in the script on your page if you are following the examples on EXIF.JS Github.

      EXIF.getData(img, function() {
                    var model = EXIF.getTag(this, "Model");
                    
                    if (model) {
                      var modelDataSpan = document.getElementById('model-'+id);
                      modelDataSpan.innerHTML = `${model}`;
                    }