0

enter image description here

I am new in lightning can anybody guide to resolve this error.

Component code --

<!-- attributes -->
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columns" type="List"/>   
<aura:attribute name="totalNumberOfRows" type="Integer" default="0"/>    
<aura:attribute name="sortedBy" type="String"/>
<aura:attribute name="sortedDirection" type="String"/>
<aura:attribute name="defaultSortDirection" type="String"/>

<!-- handlers-->
<aura:handler name="init" value="{! this }" action="{! c.doInit }"/>

<article class="slds-card slds-card_boundary">
    <div class="slds-card__header slds-grid">
        <header class="slds-media slds-media_center slds-has-flexi-truncate">
            <div class="slds-media__figure">
                <lightning:icon iconName="standard:file" alternativeText="File" size="small" />
            </div>
            <div class="slds-media__body">
                <h2 class="slds-card__header-title">
                    <a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="{!'Attachments (' + v.totalNumberOfRows + ')'}">
                        <span class="slds-text-heading_small">{!'Attachments (' + v.totalNumberOfRows + ')'}</span>
                    </a>
                </h2>
            </div>
        </header>
    </div>
    <aura:if isTrue="{!greaterthan(v.totalNumberOfRows, 0)}">
        <div class="slds-card__body slds-scrollable--y" id="attachment-table">
            <lightning:datatable columns="{! v.columns }"
                                 data="{! v.data }"
                                 keyField="id"
                                 rowNumberOffset="0"
                                 onrowaction="{! c.handleRowAction }"
                                 sortedBy="{! v.sortedBy }"
                                 sortedDirection="{! v.sortedDirection }"
                                 defaultSortDirection="{! v.defaultSortDirection }"
                                 onsort="{! c.updateColumnSorting }"
                                 hideCheckboxColumn="true"/>
        </div>
    </aura:if>
</article>    

Controller code ---

({
    doInit : function(component, event, helper) {

        var totalCnt = component.get("c.getTotalCount");
        totalCnt.setParams({
            "parentId": component.get("v.recordId")
        });
        totalCnt.setCallback(this, function(a) {
            component.set("v.totalNumberOfRows", a.getReturnValue());
        });
        $A.enqueueAction(totalCnt);       

        var actions = [
            { label: 'View', name: 'view' },
            { label: 'Delete', name: 'delete' }
        ];

        component.set('v.columns', [
            { type: 'action', typeAttributes: { rowActions: actions } },
            {label: 'Name', fieldName: 'Name', type: 'text',sortable:true },
            {label: 'Owner', fieldName: 'OwnerName', type: 'text',sortable:true },
            {label: 'Last Modified', fieldName: 'LastModifiedDate', type: 'datetime',sortable:true }
        ]);
        helper.getData(component);
    },
    // Client-side controller called by the onsort event handler
    updateColumnSorting: function (cmp, event, helper) {
        var fieldName = event.getParam('fieldName');
        var sortDirection = event.getParam('sortDirection');
        // assign the latest attribute with the sorted column fieldName and sorted direction
        cmp.set("v.sortedBy", fieldName);
        cmp.set("v.sortedDirection", sortDirection);
        helper.sortData(cmp, fieldName, sortDirection);
    },
    handleRowAction: function (cmp, event, helper) {
        var action = event.getParam('action');
        var row = event.getParam('row');
        switch (action.name) {
            case 'view':
                var attId = row.Id;               
                var url = "/servlet/servlet.FileDownload?file=" + attId;
                window.open(url, '_blank');
                break;
            case 'delete':
                var rows = cmp.get('v.data');
                var rowIndex = rows.indexOf(row);
                var deleteAct = cmp.get("c.deleteAttachments");
                deleteAct.setParams({ ids : rows[rowIndex].Id });
                $A.enqueueAction(deleteAct);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "The attachment has been delete successfully.",
                    "type": "success"
                });
                toastEvent.fire();
                rows.splice(rowIndex, 1);
                cmp.set('v.totalNumberOfRows', rows.length);
                cmp.set('v.data', rows);
                break;    
        }
    },
})

helper code---

({
    getData : function(component) {

        var action = component.get("c.getAttachmentList");
        action.setParams({
            "parentId": component.get("v.recordId")
        });
        action.setCallback(this, function(a) {
            var rows = a.getReturnValue();
            for (var i = 0; i < rows.length; i++) {
                var row = rows[i];
                if (row.Owner.Name) row.OwnerName = row.Owner.Name;
                if(row.LastModifiedDate) row.LastModifiedDate = $A.localizationService.formatDate(row.LastModifiedDate, "MM/DD/YYYY hh:mm a");
            }
            component.set("v.data", rows);
            component.set("v.currentCount", component.get("v.initialRows"));

        });
        $A.enqueueAction(action);
    },
    sortData: function (cmp, fieldName, sortDirection) {
        var data = cmp.get("v.data");
        var reverse = sortDirection !== 'asc';
        //sorts the rows based on the column header that's clicked
        data.sort(this.sortBy(fieldName, reverse))
        cmp.set("v.data", data);
    },
    sortBy: function (field, reverse, primer) {
        var key = primer ?
            function(x) {return primer(x[field])} :
        function(x) {return x[field]};
        //checks if the two rows should switch places
        reverse = !reverse ? 1 : -1;
        return function (a, b) {
            return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
        }
    }

})
-1

The getData() function in helper is trying to set value to a component attribute named currentCount, which is not available in the component markup. Since this attribute is executed in doInit, you are getting the error.

2
  • so what markupup i have to use or if i removed it than it effect the task ? – Rahul Chaudhary Feb 11 '19 at 11:18
  • component.set("v.currentCount", component.get("v.initialRows")); In the above line, both of the attributes are not defined within the component markup, why are u setting value from one attribute from another? Try commenting this line and see if it works. – dev_skv Feb 11 '19 at 11:21

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.