[英]Lightning Web Component to display JSON String is not working
I have the following lightning web component to read a JSON string and display them in Contact record Details page.我有以下闪电网络组件来读取 JSON 字符串并将它们显示在“联系人记录详细信息”页面中。 Please note that I am new to lighting web components and making a considerable amount of effort to learn.请注意,我是照明 Web 组件的新手,并付出了大量努力来学习。
MyComponent.html我的组件.html
<template>
<lightning-record-form
object-api-name={contactObject}
fields={myFields}
onsuccess={handleContactCreated} onload={handleContactInitialized} >
</lightning-record-form>
</template>
MyComponent.js我的组件.js
import { LightningElement, wire, track } from 'lwc';
import findDetails from
'@salesforce/apex/JSONDemoController.getContactWithRelatedDataById';
import CONTACT_OBJECT from '@salesforce/schema/Contact';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TEST_FIELD from '@salesforce/schema/Contact.TestField__c';
import SPOUSE_FIELD from '@salesforce/apex/ResponseJSONWrapper.spouse';
import ADDRESS_FIELD from
'@salesforce/apex/ResponseJSONWrapper.mailingAddress';
export default class ContactCreator extends LightningElement {
contactObject = CONTACT_OBJECT;
myFields = [SPOUSE_FIELD,ADDRESS_FIELD];
@track contacts;
@track error;
handleContactCreated(){
// Run code when account is created.
}
handleContactInitialized(){
findDetails()
.then(result => {
var responseObj = JSON.parse(result.getReturnValue());
this.SPOUSE_FIELD = responseObj.spouse;
this.ADDRESS_FIELD = responseObj.mailingAddress;
})
.catch(error => {
this.error = error;
});
myFields = [SPOUSE_FIELD,ADDRESS_FIELD];
}
}
JSONDemoController.cls JSONDemoController.cls
public class JSONDemoController {
@AuraEnabled
public static String getContactWithRelatedDataById() {
String response = '';
ResponseJSONWrapper wrapper = new ResponseJSONWrapper();
wrapper.spouse = 'Test Spouse';
wrapper.mailingAddress = 'Test Address';
response = JSON.serialize(wrapper);
return response;
}
} }
ResponseJSONWrapper.cls ResponseJSONWrapper.cls
public with sharing class ResponseJSONWrapper {
public String spouse;
public String contactRecordType;
public Date birthDate;
public String mobile;
public String mailingAddress;
public String otherAddress;
public String languages;
public String level;
public String Description;
}
But I don't get the values I have hard coded in the lightning component when it is rendered.但是在渲染闪电组件时,我没有得到我在闪电组件中硬编码的值。 Nothing is there it's empty.什么都没有它是空的。 Can someone help to point out where I am going wrong ?有人可以帮忙指出我哪里出错了吗?
Change this line:改变这一行:
var responseObj = JSON.parse(result.getReturnValue());
To:到:
var responseObj = JSON.parse(result);
getReturnValue() is for Aura components. getReturnValue() 用于 Aura 组件。
You don't actually need to serialize the wrapper in apex and then parse in the component explicitly, the framework does the job by itself!您实际上不需要在 apex 中序列化包装器,然后在组件中显式解析,框架会自行完成这项工作!
public class JSONDemoController {
@AuraEnabled //change return type to ResponseJSONWrapper
public static ResponseJSONWrapper getContactWithRelatedDataById() {
String response = '';
ResponseJSONWrapper wrapper = new ResponseJSONWrapper();
wrapper.spouse = 'Test Spouse';
wrapper.mailingAddress = 'Test Address';
return wrapper; //return the wrapper itself
}
and in .js file并在 .js 文件中
findDetails()
.then(result => {
var responseObj = result;
...
})
This way the code will be less cluttered with not-needed code :)这样,不需要的代码就会减少代码的混乱:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.