[英]Lightning-record-view form is not displaying the record with the id that is fetch from an apex class. Salesforce LWC,Apex
I have a record view for which the id is from apex class and then wired it and assigned to a variable, but the record is not displaying.我有一个记录视图,其 id 来自顶点 class,然后将其连接并分配给一个变量,但记录未显示。 below is my code.下面是我的代码。
apex class to get the record id of the record owned by the user apex class 获取用户拥有的记录的记录id
@AuraEnabled(cacheable=true)
public static Wallet__c getWalletInfo(){
String userId = UserInfo.getUserId();
return [SELECT Id From Wallet__c WHERE OwnerId =: userID LIMIT 1];
}
.js class - assigned the data to getId .js class - 将数据分配给 getId
import getWalletInfo from '@salesforce/apex/CustomWalletHandler.getWalletInfo';
import wallet from '@salesforce/schema/Wallet__c';
import walletName from '@salesforce/schema/Wallet__c.Name';
import walletBalance from '@salesforce/schema/Wallet__c.balance__c';
import walletFrom from '@salesforce/schema/Wallet__c.added_from__c';
export default class CustomWallet extends LightningElement {
objectName = wallet;
name = walletName;
balance = walletBalance;
from = walletFrom;
getId;
isUser = true;
@wire (getWalletInfo,{})
walletData({error, data}){ //data is record Id
if(data){
this.getId = data;
console.log(this.getId);
this.isUser = false;
}else if(error) {
// error handling
console.error(error.body.message);
}
}
}
html - if user has no recordc Create Wallet will display and if user has record only the record will display. html - 如果用户没有记录,将显示创建钱包,如果用户有记录,则仅显示记录。
<template>
<lightning-card>
<lightning-tabset>
<template if:true={isUser}>
<lightning-tab label="Create a Wallet">
<lightning-record-form
object-api-name="Wallet__c"
columns="2"
layout-type="Full"
></lightning-record-form>
</lightning-tab>
</template>
<template if:false={isUser}>
<lightning-tab label="My Wallet">
<lightning-record-view-form
record-id={getId}
object-api-name={objectName}>
<div class="slds-box">
<lightning-output-field field-name={name}>
</lightning-output-field>
<lightning-output-field field-name={balance}>
</lightning-output-field>
<lightning-output-field field-name={from}>
</lightning-output-field>
</div>
</lightning-record-view-form>
</lightning-tab>
</template>
</lightning-tabset>
</lightning-card>
I think you overcomplicated it a bit.我认为你有点过于复杂了。 What do you get when you put simpler references to object & fields?当您对 object 和字段进行更简单的引用时,您会得到什么?
<lightning-record-view-form
record-id={getId}
object-api-name="Wallet__c">
<div class="slds-box">
<lightning-output-field field-name="Name"></lightning-output-field>
<lightning-output-field field-name="Balance__c"></lightning-output-field>
<lightning-output-field field-name="Added_From__c"></lightning-output-field>
</div>
</lightning-record-view-form>
And your apex method is returning whole Wallet__c
object, not just the Id.而且您的顶点方法返回整个Wallet__c
object,而不仅仅是 Id。 With just 1 field populated but still.仅填充了 1 个字段,但仍然存在。 You can tell by seeing curly braces in your console.log's output.您可以通过在 console.log 的 output 中看到花括号来判断。 So you need either this.getId = data.Id;
所以你需要this.getId = data.Id;
in JS or record-id={getId.Id}
in HTML.在 JS 或 HTML 中的record-id={getId.Id}
中。 Or make Apex return just an Id variable (and null if no results found I guess?)或者让 Apex 只返回一个 Id 变量(如果我猜没有找到结果,还有 null?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.