2

I have been stuck on this for a few hours, hope someone will be able to help me with this. I simplified the code as well.

This is the error I am getting => [Cannot read property 'firstNameFieldLabel' of undefined]. But I am able to log the values in JS file with no error.

//LWC
    <template>    
        I am here ==> {Wrapper.firstNameFieldLabel}
    </template>

// Javscript Snippet
import ApplicationWrapper from '@salesforce/apex/ApplicationCtrl.getApplicationWrapper';
Wrapper;
  @wireApplicationWrapper 
  wiredWrappers({error, data }){
    if(data){
      this.Wrapper = data;
    }
    else{
      console.log(error);
    }
  }


//Wrapper class
public with sharing class ApplicationCtrl{
    public class ApplicationWrapper{
        @AuraEnabled public String firstNameFieldLabel {get;set;}
        @AuraEnabled public String lastNameFieldLabel {get;set;)
        public ApplicationWrapper(String firstNameFieldLabel, String lastNameFieldLabel){
            this.firstNameFieldLabel = firstNameFieldLabel;
            this.lastNameFieldLabel = lastNameFieldLabel;            
        }
    }

   @AuraEnabled(cacheable = true)
    public static ApplicationWrapper getApplicationWrapper(){
        ApplicationWrapper Wrapper = new ApplicationWrapper('First Name', 'Last Name');  
        return Wrapper;
    }

}

1 Answer 1

2

There are couple of minor issues with the front end code

  1. Use track on the Object property to make it reactive.
  2. Initialize the object.
  3. Also be careful with the names of the property and the component. Do not capitalize your variable names.

The below code works fine in my scratch org

 import { LightningElement, wire, track } from 'lwc';

 import applicationWrapper from '@salesforce/apex/ApplicationCtrl.getApplicationWrapper';

 export default class AppWrapper extends LightningElement {

   @track
   wrapper = {};

   @wire(applicationWrapper)
   wiredWrappers({error, data }){
    if(data){
        console.log(JSON.stringify(data));
        this.wrapper = data;
    }
    if(error) {
        console.log(JSON.stringify(error));
    }
  }
}

The Template code

<template>
   I am here ==> {wrapper.firstNameFieldLabel}
</template>

Note i have named the component appwrap and files are appwrap.lwc and appwrap.js

2
  • Thanks Mohith! I just had to initialize it, I don't think capitalization or track were an issue (I tried with and without, it worked) Tx!! Stay Safe!
    – malu
    Apr 4, 2020 at 0:31
  • 1
    You too . Take care ! Capatalization is not an issue from run time perspective but just not good practice to capatlize the variables. Apr 4, 2020 at 0:53

You must log in to answer this question.

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