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 ... 


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

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

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);

    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);


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

        parentId: component.get("v.parentId"),
        fileName: file.name,
        base64Data: encodeURIComponent(fileContents), 
        contentType: file.type

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



Apex class:

  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]'.


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

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

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.

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.