0

I have a data table which displays Name field and Address field(Address related to another object).The Name field is displayed properly but if I iterate over the list in html for pcon.Address__r.Name it gives error. If only pcon.Address__r is given, it shows [object object] in table. Can anybody resolve this issue. thank you in advance.

html

    <table class="slds-table slds-table--bordered">
        <thead>
            <tr>
                <th style="background-color: #151963;color: white;">Name</th>
                <th style="background-color: #151963;color: white;">Address</th>
            </tr>
        </thead>
        <tbody>
            <template for:each={pconfig} for:item="pcon" for:index="i">
                <tr class={pcon.row} data-id={pcon.Id} data-index={i} key={pcon.Id} onclick{getquotes} >
                    <td>{pcon.Name}</td>
                    <td>{pcon.Address__r.Name}</td>
                </tr>
            </template>
        </tbody>
    </table>
</lightning-layout-item>

Js:

@wire(Vendor_getProduct,{ basketId: 'fef0E0000dwedjh' })
    getProducts({error,data})
    {
         if(data)
         {
            //console.log( 'Fetched Data ' + JSON.stringify( data ) );
            this.pconfig =  JSON.parse( JSON.stringify( data) );
            console.log('products are:',data);
         }
         else if(error){
            this.error=data;
            console.log('error is undefined');
         } 
    }

apex class:

public class c {
    @AuraEnabled(cacheable=true)
    public static List<Product_Configuration__c> getProduct (Id basketId) {
        return [select  id, 
                        name, 
                        Address__r.name, 
                from  Product_Configuration__c
 where Product_Basket__c =:basketId];
    }
3
  • What is the output of this log? console.log('products are:',data); Commented Oct 28, 2022 at 8:53
  • 1
    i saw some mistake in above code like in apex remove comma after Address__r.name and in html write method in quotes onclick{getquotes} Commented Oct 28, 2022 at 9:00
  • Address__c: "a3d0E000002p8eZQAQ" Address__r: Id: "a3d0E000002p8eZQAQ" Name: "Test account 1" [[Prototype]]: Object Bandwidth__c: 40 CurrencyIsoCode: "USD" Id: "a3J0E0000017WwcUAE" Name: "Big Blue" Service_Bandwidth__c: 40 cscfga__Product_Basket__c: "a3G0E000001Ui6rUAC" [[Prototype]] : I need to print address name i.e Test account 1 Commented Oct 28, 2022 at 11:03

1 Answer 1

1

in js do not need to parse or stringify

use track decorator to define the variable like this @track pconfig = [];

this will resolve your problem

js :

@wire(Vendor_getProduct,{ basketId: '0032w000006LmH9AAK' })
    getProducts({error,data})
    {
         if(data){ 
            this.pconfig = data;
            console.log(this.pconfig)
        }
        if(error){
            console.error(error)
        }
    }
3
  • Hi Karan Sharama , I have tried the way you had shared. even that doesnt work Commented Oct 28, 2022 at 11:05
  • 1
    can you show me the error. Commented Oct 28, 2022 at 11:17
  • [Cannot read properties of undefined (reading 'name')] is the error Commented Oct 29, 2022 at 6:15

You must log in to answer this question.

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