1

I have an LWC that uses the wire below to query for an opportunity using an ID. Once I get the results, I'm struggling to assign the value of one of the fields returned by the wire to a variable. The returned value from apex is a list even though it will always return just one record.

here is LWC:

//wire to get opportunity info.  Specifically the quotingdental field to display dental and bundled products
@wire(fetchOpp, {oppid: '$recordId'})
    opps(data, error) {
      
      
      if (data) {
        console.log('$$ here is the data ' + JSON.stringify(data));
 //This is the line I need help with.      
      this.quotingDental = this.data.Quoting_Dental__c;
          //since we have data, update error to undefined
        this.error = undefined;

      }
      else if (error) {
          this.error = error;
          this.quotingDental = undefined;
        }
          //needed to udpate data when there are no records to display - specificallly added when you delete the last record
      else{
          this.quotingDental = undefined;
          this.error = undefined;

        }
  }   

Here is what the returned data looks like:

$$ here is the data

{
  "data": [
    {
      "Id": "006S000000MNxxxxx",
      "Name": "Test1",
      "OwnerId": "005S000000NYxxxxx",
      "CloseDate": "2030-01-01",
      "StageName": "Broker Prep",
      "Quoting_Health__c": "No",
      "Quoting_Dental__c": "Yes",
      "Quoting_Vision__c": "No",
      "Quoting_RX__c": "No",
      "Quoting_Vol_Dental__c": "No",
      "Quoting_Vol_Vision__c": "No",
      "Quoting_Life_DI__c": "No",
      "Owner": {
        "Name": "frank",
        "Id": "005S000000NYJxxxxx",
        "CommunityNickname": "frank"
      }
    }
  ]
}

1 Answer 1

0

You only use this to access class-level properties (data, functions, etc); for the local parameter data, it's just data. In addition, Arrays have indices, so you have to access the data by its index.

  this.quotingDental = data[0].Quoting_Dental__c; // zero-based index
2
  • I have tried that but I get the following error: Uncaught (in promise) TypeError: LWC component's @wire target property or method threw an error during value provisioning. Original error: [Cannot read properties of undefined (reading 'Quoting_Dental__c')]
    – Frank
    Commented Dec 1, 2021 at 18:47
  • 1
    @Frank You have an additional bug in your code: opps(data, error) { should be opps({data, error}) {. Make sure you pay close attention to the documentation, little things like that can trip you up.
    – sfdcfox
    Commented Dec 1, 2021 at 18:59

You must log in to answer this question.

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