2

So I am using a website to add functionality of attaching a file using a lightning component. http://peterknolle.com/file-upload-lightning-component/

The problem I'm having is I get an error message when I refresh Lightning:

Uncaught Error in $A.getCallback() [Cannot read property 'apply' of undefined]
Callback failed: aura://ComponentController/ACTION$getComponent
 throws at ... 

Component:

<aura:attribute name="parentId" type="Id"/>  
<aura:handler event="aura:waiting" action="{!c.waiting}"/> 
<aura:handler event="aura:doneWaiting" action="{!c.doneWaiting}"/> 

<div> 
    <input type="file" class="file" aura:id="file" />
    <ui:button label="Save" press="{!c.save}"/>
</div>

Controller (minor changes from website):

({

 save : function(component) {
      var MAX_FILE_SIZE = 750 000;
    var fileInput = component.find("file").getElement();
    var file = fileInput.files[0];

    if (file.size > this.MAX_FILE_SIZE) {
        alert('File size cannot exceed ' + this.MAX_FILE_SIZE + ' bytes.\n' +
              'Selected file size: ' + file.size);
        return;
    }

    var fr = new FileReader();

    var self = this;
    fr.onload = function() {
        var fileContents = fr.result;
        var base64Mark = 'base64,';
        var dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;

        fileContents = fileContents.substring(dataStart);
        self.upload(component, file, fileContents);
    };

    fr.readAsDataURL(file);
},

upload: function(component, file, fileContents) {
    var MAX_FILE_SIZE = 750 000;
    var action = component.get("c.saveTheFile");

     action.setParams({ 
        parentId: component.get("v.parentId"),
        fileName: file.name,
        base64Data: encodeURIComponent(fileContents), 
        contentType: file.type
    });

     (action.setCallback(this, function(a)) {
        attachId = a.getReturnValue();
        console.log(attachId);
    });

    $A.enqueueAction(action);     
}

})

Apex class:

  @AuraEnabled
  public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) { 
    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');

    Attachment a = new Attachment();
    a.parentId = parentId;

    a.Body = EncodingUtil.base64Decode(base64Data);
    a.Name = fileName;
    a.ContentType = contentType;

    insert a;

    return a.Id;
}

Not sure why I get this error message. I don't have a variable called 'apply' so I'm not sure what this means '[Cannot read property 'apply' of undefined]'.

1 Answer 1

7

Looks like you haven't declared attachId that you are using in your callback.

(action.setCallback(this, function(a)) {
        **var attachId** = a.getReturnValue();
        console.log(attachId);
    });

When you get the cannot apply message, it is actually just a vague message that you couldn't assign something for some reason or another.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .