【问题标题】:Salesforce, LWC, Lightning App Builder, Cannot read propertySalesforce、LWC、Lightning 应用程序生成器、无法读取属性
【发布时间】:2021-03-12 12:01:33
【问题描述】:

我将组件创建为 Lightning Web 组件。在 VSCode 中没问题。 当我在 Lightning App Builder 中添加此组件时,有一个错误: "LWC 组件连接阶段出错:[Cannot read property 'fields' of undefined]"

product.html:

<template>
  <div class="container">
      <a onclick={productClick}>
        <div class="product">{name}</div>
      </a>
      <a onclick={addToCart}>
        <div class="product">Add to cart</div>
      </a>
  </div>
</template>

product.js:

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

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

export default class Product extends LightningElement {
  @api item; // public property
  item = recordId;

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

  get name() {
    console.log('Returned value: ' + this.account.data.fields.Name.value);
    return this.account.data.fields.Name.value;
  }

  productClick() {

  }

  addToCart() {
    
  }
}

我使用 API v.51。根据this manual编程。

【问题讨论】:

    标签: salesforce lwc


    【解决方案1】:

    HTML 尝试首先加载(以尽快向用户显示一些东西,任何东西。也许是一些占位符,也许是加载图标...)。这几乎是纯 html,您的 @wire 尚未返回数据,因此您的 this.contact 未定义。然而 HTML 会调用 getter,所以 null.fields 会导致错误。

    尝试将 HTML 包装在 &lt;template if:true&gt; 中,或者在所有 getter 中编写类似 return this.account &amp;&amp; this.account.data ? this.account.data.fields.Name.value : null; 的内容

    查看https://developer.salesforce.com/forums/?id=9062I000000QwLGQA0 中的最后一个答案,或者这也不错(但@wire 的用法有点不同):https://salesforce.stackexchange.com/questions/264669/how-to-avoid-error-cannot-read-property-of-undefined-using-two-lwc-wired-propert

    【讨论】:

    • 对象Account is filled。而且自定义对象也无法读取。是否需要任何阅读权限?可能,我在哪里可以检查访问权限?
    • 嘿@Elo,这个答案是正确的,您必须先检查帐户数据,然后才能访问它的属性。线路异步运行,因此可能存在 this.account 没有任何数据的时间跨度......所以在那个时候,如果渲染发生也是异步的,getter 方法调用将访问 this.account (如果为空)会给你错误。
    【解决方案2】:

    这是编辑后的代码: 产品.html:

    <template>
      <p>LWComponent</p>
      <template if:true={account}>
        <div class="container">
            <a onclick={productClick}>
              <div class="product">{name}</div>
              <!--<img class="product-img" src={product.fields.Picture_URL__c.value}></img>-->
            </a>
            <a onclick={addToCart}>
              <div class="product">Add to cart</div>
            </a>
        </div>
      </template>
    </template>
    

    product.js:

    import { LightningElement, api, wire } from 'lwc';
    import { getRecord } from 'lightning/uiRecordApi';
    
    const FIELDS = [
      'Account.Name',
      'Account.Phone'
    ];
    
    export default class Product extends LightningElement {
      @api recordId;
    
      @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
      account;
    
      get name() {
        console.log("Yes, I'm here :)");
        if (this.account) {
          console.log('this.account: ' + JSON.stringify(this.account));
        } else {
          console.log("'this.account' does not exist.");
        }
        if (this.account.data) {
          console.log('this.account.data: ' + JSON.stringify(this.account.data));
        } else {
          console.log("'this.account.data' does not exist");
        }
        if (this.account.fields) {
          console.log('this.account.fields: ' + JSON.stringify(this.account.fields));
        } else {
          console.log("'this.account.fields' does not exist");
        }
        return this.account && this.account.data ? this.account.data.fields.Name.value : null;
        //return this.account.data.fields.Name.value;
      }
    }
    
    

    控制台列表:

    Yes, I'm here :)
    this.account: {}
    'this.account.data' does not exist
    'this.account.fields' does not exist
    

    帐户没什么,我不明白为什么。

    【讨论】:

    • 您的@wire 监听$recordId 的动态变化,这是它的输入参数。但是您没有定义 @api recordId; 变量,因此没有什么可听的,也没有什么可以根据的。 “recordId”和“objectApiName”是两个特殊变量,developer.salesforce.com/docs/component-library/documentation/…
    • 需要从数据库中添加变量recordId Id:@api recordId = 'a010900000giYo0AAE';这是它的工作原理。
    【解决方案3】:

    我添加了'@api recordId;' 以及我编辑的文件 product.js-meta.xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
      <apiVersion>51.0</apiVersion>
      <isExposed>true</isExposed>
      <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
        <target>lightningCommunity__Default</target>
      </targets>
      <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
          <property
            name="recordId"
            type="String"
            label="Record Id"
            description="Automatically bind the page's record id to the component variable"
            default="{!recordId}" />
        </targetConfig>
      </targetConfigs>
    </LightningComponentBundle>
    
    

    结果是一样的。在对象帐户中什么都不是。

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 1970-01-01
      • 2021-11-03
      • 2020-03-26
      • 2018-12-13
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多