2

This seems like it should be really simple but I am finding it impossible to get wire field values into a variable I can then use within the Javascript code (such as concatenating them into a String to be displayed as the LWC title).

I can easily call each field value directly from HTML, but as soon as I try to reference it with this.variablename I get a 'Cannot read properties of undefined (reading 'data')' error.

import { LightningElement, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import PROD from '@salesforce/schema/Product__c.Name';

const FIELDS = ['Product__c.Name'];

@wire(getRecord, { recordId: '$prodid', fields: FIELDS })
product;

get productname() {
     return getFieldValue(this.product.data, PROD);
}
    
teststring = 'Product: '+this.productname;

2 Answers 2

0

As mentioned in documentation here
You must add the fields parameter to getRecord method !!

Here the example :

import NAME_FIELD from '@salesforce/schema/Account.Name';

const FIELDS = ['Account.Name', 'Account.Phone'];

@wire(getRecord, { recordId: '$recordId' , fields: FIELDS})
accoun;

get productname() {
    return getFieldValue(this.accoun.data ,  NAME_FIELD );
}

handleClick(){
    console.log(this.productname)
}
get testString() {
   return 'Product :' +this.productname;
} 
5
  • Sorry must of accidentally taken that out! My issue isn't referencing the getter in HTML (e.g. calling {productname}). The issue is the final line where I reference this.productname in javascript. It keeps either giving me undefined data error or just being blank on the LWC.
    – Jonathan
    Commented Nov 27, 2021 at 9:12
  • @NewToThis ,The reason is your getter is getting executed only at last line and since the prod field value is not imported using '@salesforce/schema/objectName.prod' in JavaScript so the reference to the this.product.data is incorrect Which in turns resulting in undefined of some error. Commented Nov 27, 2021 at 18:21
  • So I added in the initial lines that imported the field values and made some changes as per Vitalii's suggestions. The issue is the final line still. If I take it out and reference {productname} it pulls through the value fine. But if I add in the last line and reference {teststring} it is just blank and if I reference {productname} with that line in the code it gives me an undefined error.
    – Jonathan
    Commented Nov 28, 2021 at 15:53
  • @NewToThis How exactly you want to call this.productname ? I edited the example? Commented Nov 29, 2021 at 9:21
  • I cant believe it was as easy as making teststring a getter.... Thank you so much!
    – Jonathan
    Commented Nov 29, 2021 at 10:02
0

You are the wire adapter is asynchronous, so when the getter is first used, there is probably no value (or there could be an error... more about that later).

Generically, Wired ServiceSyntax is

import { adapterId } from 'adapterModule';


@wire(adapterId, adapterConfig)
propertyOrFunction;

(see: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/data_wire_service_about)

Specifically:

This is the textbook example. When the recorded populated, wire will fire getting the data you request (you can add fields to the array), caching it in the Lightning Data Service. In the HTML, the template waits until data or error is populated. c-error-panel is a component from LWC Receipes. I strongly suggest you clone the repo, there are lots of examples and some good starting points for your LWC adventure!

// wireFunction.js
import { LightningElement, api, track, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class WireFunction extends LightningElement {
    @api recordId;
    @track record;
    @track error;

    @wire(getRecord, { recordId: '$recordId', fields: ['Prod.Name'] })
    wiredAccount({ error, data }) {
        if (data) {
            this.record = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.record = undefined;
        }
    }
    get name() {
        return this.record.fields.Name.value;
    }
}

and

<!-- wireFunction.html -->
<template>
    <lightning-card title="Wire Function" icon-name="standard:contact">
        <template if:true={record}>
            <div class="slds-m-around_medium">
                <p>{name}</p>
            </div>
        </template>
        <template if:true={error}>
            <c-error-panel errors={error}></c-error-panel>
        </template>
    </lightning-card>
</template>
4
  • See developer.salesforce.com/docs/component-library/documentation/… for getFieldValue. This works when used correctly, no need to write a custom handler and all that.
    – sfdcfox
    Commented Nov 26, 2021 at 17:54
  • @sfdcfox, there are lots of ways to skin this vegan cat.* I was simply giving them the example from the documentation, I wouldn't use a getter either, but since they already had one and the documentation referenced it, I decided to limit my answer. *no animals, real or imaginary, were harmed in the making of this comment. Commented Nov 26, 2021 at 18:02
  • Thanks for the detailed response. Unfortunately its not exactly the issue I am having. I'm happy using wire in the way above and for your example could reference the Name field in the HTML by calling {name}, however my issue is when I go to reference it in the Javascript file by calling this.name. I then get the following error 'Cannot read properties of undefined (reading 'data')'.
    – Jonathan
    Commented Nov 26, 2021 at 23:30
  • you need to show your code. you probably are probably having an issue either in your wire call (debug this.error) or you are calling the getter before you have data (add a null check). Commented Nov 28, 2021 at 13:37

You must log in to answer this question.

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