Behrouz Riahi
Behrouz Riahi

Reputation: 1801

"Uncaught Error: Match error: Expected string, got undefined" when uploading an image

I got this error when uploading am image

match.js:38 Uncaught Error: Match error: Expected string, got undefined
check @ match.js:38
_addUrlToDatabasePI @ upload-to-amazon.js:32
(anonymous function) @ upload-to-amazon.js:108
(anonymous function) @ edgee_slingshot.js:375

if am not mistaken, it's related to the way am using check in my _addUrlToDatabasePI method. here is my method

let _addUrlToDatabasePI = ( url ) => {
  check(url, String);
  var previewImageURL = Session.get("previewImageURL");
  previewImageURL = url;
  Session.set("previewImageURL", previewImageURL);
  console.log("Test" + " " + Session.get("previewImageURL"));
  _afterUploadingPI();
};

uploader is used with slingshot methods that will upload the image in Amazon S3,

var uploader

      if (config === '1') {
          uploader = new Slingshot.Upload( "uploadToAmazonS3Cg1" );
      } 
      if (config === '2') {
          uploader = new Slingshot.Upload( "uploadToAmazonS3Cg2" );
      }
      if (config === '3') {
          uploader = new Slingshot.Upload( "uploadToAmazonS3Cg3" );
      }

Here how am using the _addUrlToDatabasePI function, it is on the client side,

uploader.send( file, ( error, url ) => {
    if ( error ) {
      Bert.alert( error.message, "warning" );
      _setPlaceholderText();
    } 
    if (config === '1'){
      _addUrlToDatabasePI( url );
    }
    if (config === '2') {
      _addUrlToDatabaseSS( url )
    }
    if (config === '3') {
      _addUrlToDatabaseZip (url )
    }
  });
};

it is weird because it used to work and i could upload files with no errors, its suddenly appeared. i shut down my laptop yesterday and today when i tried to test it. it is working. this is weird!!

any idea what might cause the error?

Upvotes: 0

Views: 2676

Answers (1)

Serkan Durusoy
Serkan Durusoy

Reputation: 5472

uploader.send works asynchronously so the url is not immediately available in its callback. You should first check for its existence:

uploader.send( file, ( error, url ) => {
    if ( error ) {
      Bert.alert( error.message, "warning" );
      _setPlaceholderText();
    }
    if ( url ) { // check that url has returned
        if (config === '1'){
          _addUrlToDatabasePI( url );
        }
        if (config === '2') {
          _addUrlToDatabaseSS( url )
        }
        if (config === '3') {
          _addUrlToDatabaseZip (url )
        }
    }
  });
};

Upvotes: 1

Related Questions