I am new to salesforce, I am trying to learn lightning datatable with imperative call in salesforce. I am unable to get the data in the column, its not throwing any error though. Please help to fix it. Below is the code:
''' HTML Code:
<template>
<lightning-datatable
key-field="Id"
data={result}
columns={columns}></lightning-datatable>
</template>
''' Javascript:
import { LightningElement, track } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getData';
const columns = [
{ label: 'First Name', fieldName: 'FirstName' },
//{ label: 'Last Name', fieldName: 'LastName' },
//{ label: 'Email', fieldName: 'Email', type: 'email' },
];
export default class DataTableWithImperativeCall extends LightningElement {
@track result=[];
@track columns = columns;
connectedCallback() {
this.getResults();
}
getResults() {
getContactList()
.then(cdata => {
//console.log(cdata);
console.log(JSON.stringify(cdata));
let finalData=[];
for(var i=0;i<cdata.length;i++)
{
finalData.push(cdata[i].FirstName);
}
this.result=finalData;
//console.log('results'+JSON.stringify(this.result));
})
.catch(error => {
this.errors = error;
});
}
}
''' Apex Class:
public with sharing class ContactController {
@AuraEnabled
public static list<contact> getData()
{
return [select id,firstName,LastName,email from contact order by firstName asc nulls last limit 10];
}
}
'''