5

I am trying to access JS object properties in the client controller. I get undefined if I try access it by name. But I see the properties if I log the whole object. Something I am missing?

  var uploadedFiles = event.getParam("files");
    console.log(JSON.stringify(uploadedFiles));
    //var objectParsed = JSON.parse(uploadedFiles);
    var docId = uploadedFiles["name"];
    console.log(docId);
    for (var key in uploadedFiles) {
          if (uploadedFiles.hasOwnProperty(key)) {
            console.log(key);
          }
        }

Console output

enter image description here

3

Your JSON [{"name":"xxx","documentId":"xxx"}] looks like an array, not an object.

Change the way you get the docId to

var docId = uploadedFiles[0]["name"]; or var docId = uploadedFiles[0].name;

and the loop to

for (var key in uploadedFiles[0]) {

  • Thank you! SF Documentation says the event.getParam() returns a JS object, not an array. But that worked. Thanks again! – Allen Mann Oct 31 '17 at 20:26
  • :) You never know... it pays to be eagle eyed when checking your debug logs. I've gone through this same thing too many times to count. – Sebastian Kessel Oct 31 '17 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.