Cannot read property 'data' of undefined - LWC
I'm trying to work with Map in LWC. I have three lightning cards(Each card will have unique identifier. for ex: Key from my map) in my component. I wanted to iterate over the map and fetch value based on key and show it on each lightning card. But even before I can use the map in HTML I'm getting below error when my component is rendered.However, map is returning the expected values.
Error during LWC component connect phase: [Cannot read property 'data' of undefined]
Below is my code:
js code
import { LightningElement, wire , track} from 'lwc'; import controllerMEthod from '@salesforce/apex/myController.controllerMEthod'; export default class myCmponentLWC extends LightningElement { @track mapData=[]; @wire(controllerMEthod) wrprMap(result) { if (result.data) { var conts = result.data; for(var key in conts){ this.mapData.push({value:conts[key], key:key}); } } } }
Apex code
public with sharing class myController{ public static Map wrprMap = new Map(); @AuraEnabled(cacheable=true) public static Map controllerMEthod() { for(object1__c obj : [SELECT Id, field2__c ,field3__c,field4__c FROM object1__c]){ wrapperClass = new wrapperClass(); wrpr.string1= obj.field2__c ; wrpr.booleanVal = String.valueOf(obj.field3__c); wrpr.string2= obj.Id; wrpr.string3= obj.field4__c; wrprMap.put(wrpr.string1, wrpr); } system.debug('wrprMap---------'+wrprMap); return wrprMap; } public class wrapperClass{ @AuraEnabled public string string1; @AuraEnabled public Boolean booleanVal{get; set;} @AuraEnabled public String string2; @AuraEnabled public String string3; } }
The method may not initially return any data before hitting the server, so you should check for that first:
if(result && result.data) { this.mapData = Object.entries(result.data).map(([key,value]) => ({key,value})); }
Here we have trouble shooting steps to resolve error “cannot read property 'data' of undefined”.
Troubleshooting process
Determine the component
First of all, you need to determine which components on the screen lead to standard library alarms. This step is simpler because it is used on the screen.record-edit-form There are only two components. After a slight review, I found that two components will throw this error.
Analytical components
The component contains two parts, one isrecord-edit-form In uselightning-output-field Display the fields of the object, one is usedlightning-input-field To call up the input box of the field, let the user enter information. According to the error message, it seems that there is a problem.displayValue , This is cooked, because in my JS file, I will use the LWC standard API.getRecord To manually extract the field value, in the extraction process, you will use similar:data.fields.fieldName__c.displayValue To save the obtained value. Will not be taken this placeundefined What about the field?
Note JS code
According to the above analysis, I will bring all the jsdisplayValue The code comment. Unfortunately, the warning information did not disappear.
Direction to HTML code
If it is not a warning caused by the API in the JS, then the problem is likely to be in the HTML code. First of all, I think of the problem of physical names. If the physical name of the field is inconsistent with the background, it will also cause a similar error. So I willoutputField Each field that appears is compared with the background definition. There is no error. Then, I also compared the permissions of the object field in the profile, still there is no problem.
First lock field
Since all the attempts above failed, I can only use stupid methods, because I noticed that this warning message will appear without each field, actually there is only one field in my component. Therefore, first find that the field will help to find the problem. Thus, in the HTML file, the "two-point method" is used to lock the field of problems. Then the background definition of this field is compared with the normal field.
Find the key
After I lock the field, find another same type, but will not alarm the field, compare the difference between the two, and there is nothing difference at first glance. But when Field accessibility In, I finally found the spider silk.
Hope this will help!