I am really having a weird issue where I am trying to simply render a couple of records on a LWC table. I am getting all the wired data right and I believe all is setup correct. Don't know what I am missing.
The data wont show on the screen.
import { LightningElement,wire } from 'lwc';
import relatedProducts from '@salesforce/apex/OpportunityRelatedProducts.getOpportunityProducts';
const COLS = [
{
label: 'Name',
fieldName: 'Name',
type: 'text'
}, {
label: 'Price Book',
fieldName: 'PricebookEntryId',
type: 'text'
}, {
label: 'Quantity',
fieldName: 'Quantity',
type: 'number'
}, {
label: 'Unit Price',
fieldName: 'UnitPrice',
type: 'number'
}
];
export default class LineItemTable extends LightningElement {
results=[];
columns = COLS;
@wire(relatedProducts) lineitems({error,data}){
if(data){
console.log('****Data is: '+data);
data.forEach(record => {
this.results.push(record);
});
console.table(this.results);
}
else if(error){
console.log('****There was an error');
}
}
renderedCallback(){
console.log('****Inside rendered');
}
}
<template>
<template if:true={results}>
<div>
<lightning-datatable columns={columns} data={results} key-field="Id"></lightning-datatable>
</div>
</template>
</template>
public with sharing class OpportunityRelatedProducts {
@AuraEnabled(Cacheable = true)
public static list<OpportunityLineItem> getOpportunityProducts() {
try {
return [select Id,Name, PricebookEntryId,Quantity,UnitPrice from opportunitylineitem where opportunityid = '0067d00000HIQBeAAP' ];
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
}