Can’t get object property in lightning component controller

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

Answer

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]) {

Attribution
Source : Link , Question Author : Allen Mann , Answer Author : Sebastian Kessel

Leave a Comment