Salesforce、LWC、Lightning App Builder无法读取属性
我将该组件创建为Lightning Web组件。在VSCode中,这是可以的。 在Lightning App Builder中添加此组件时,存在一个错误: “LWC组件连接阶段出错:[无法读取未定义的属性'fields'” product.html:Salesforce、LWC、Lightning App Builder无法读取属性,salesforce,lwc,Salesforce,Lwc,我将该组件创建为Lightning Web组件。在VSCode中,这是可以的。 在Lightning App Builder中添加此组件时,存在一个错误: “LWC组件连接阶段出错:[无法读取未定义的属性'fields'” product.html: <template> <div class="container"> <a onclick={productClick}> <div class=&quo
<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>
<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>
我使用API v.51。它是根据。编程的。HTML首先尝试加载(以便尽快向用户显示某些内容,任何内容。可能是一些占位符,可能是加载图标…)。这几乎是纯html,您的
@wire
尚未返回数据,因此您的This.contact
未定义。但是HTML调用getter,因此null.fields
会导致错误
尝试在
中包装HTML,或者在所有getter中编写类似的内容返回this.account&&this.account.data?this.account.data.fields.Name.value:空代码>
请参阅中的最后一个答案,或者这也很不错(但这是因为@wire
的用法有点不同):这是经过编辑的代码:
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>
<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>
控制台列表:
Yes, I'm here :)
this.account: {}
'this.account.data' does not exist
'this.account.fields' does not exist
帐户里什么都不是,我不明白为什么。我添加了'@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>
51
真的
闪电应用页面
闪电记录页
闪电主页
lightningCommunity\uuuu默认值
结果是一样的。对象中的帐户为空。对象。而且自定义对象也无法读取。它是否需要任何读取权限?可能,我在哪里可以检查访问权限?嘿@Elo,这个答案是正确的,您必须先检查帐户数据,然后才能访问它的属性。该连接以异步方式运行,因此可能存在this.account没有任何数据的时间跨度。。。因此,在这段时间内,如果呈现也是异步的,则getter方法将调用,该方法将访问this.account(如果为null),并将给出错误。您的@wire
侦听对$recordId
的动态更改,它是该方法的输入参数。但是您没有@api recordId定义了代码>变量,这样就没有什么可听的,也没有什么可根据它来提取数据。“recordId”和“objectApiName”是两个特殊变量,需要从数据库中添加变量recordId:@api recordId='a010900000giYo0AAE'代码>这就是它的工作原理。