简体   繁体   English

显示 JSON 字符串的 Lightning Web 组件不起作用

[英]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.

相关问题 Lightning Web 组件设置动态样式未按预期工作 - Lightning Web Component setting dynamic style not working as expected <noerrorobjectavailable>闪电 web 组件 (LWC) 中的脚本错误</noerrorobjectavailable> - <NoErrorObjectAvailable> Script error in lightning web component (LWC) 如何在 lightning web 组件中创建弹出窗口 - How to create popup in lightning web component Lightning Web 组件 IF 指令是否支持多个条件? - Does Lightning Web Component IF Directive support multiple conditions? 401 未经授权 REST API 中闪电 web 组件 ZA8FF6C9514C35C8563B350382 - 401 Unauthorized REST API in lightning web component salesforce 在没有顶点的闪电 web 组件上创建任务仅 javascript controller? - Create a task on a lightning web component without apex only javascript controller? 输入有效的 URL 并重试: Salesforce Lightning Web 组件错误 - Enter a valid URL and try again : Salesforce Lightning Web Component Error 如何在 Visual Force Pages 中使用 Lightning Web 组件? - How to use Lightning Web Component inside Visual Force Pages? lightning web组件中如何使用reducerrors方法处理错误? - how to use reducerrors method to handle errors in lightning web component? 如何获取闪电组件salesforce中的查询字符串参数 - how do I get the query string parameters in lightning component salesforce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM