Error: afterRender threw an error in ‘c:viewRecord’ [Cannot read property ‘fields’ of undefined]

If you are new and learning LWC , you might face this error.
I was doing everything right but still the error pop’s up and I have struggled to find the answer. It took me few days to realize what can be an issue

lwc error

 

Solution:
When you trying to access the data , it might return the null value and therefore undefined error is returned.

To overcome this you need to put a check on the data.

Example:
if(this.account.data){
        return this.account.data.fields.Name.value;
      }
     return undefined;