首页 > 解决方案 > 空 JSON 值 (LWC)

问题描述

大家
好,我正在尝试从 apex 方法中获取一些“硬编码”值,但是当我编写 console.log 时,它是空的。 在此处输入图像描述

这是我正在处理的代码:

@wire(getValues)
wiredValues({error, data})
     if(data) {
     console.log("Data::::::",data);
     this.getVal = JSON.stringify(data);
     } else if(error){
           this.error = error;
           this.getVal = undefined;
           console.log("No values");
     }

这是我的顶点方法(我正在尝试制作一种“假”标注,但我不确定我是否正确):

public without sharing class getSomeValues {
       @AuraEnabled(cacheable = true)
       public static List<wrapVal> getWrapVal() {

        HttpResponse request = new HttpResponse();
        request.setBody('{"Values": ["1000", "2000", "3000", "4000", "5000"]}');
        Map<String, Object> results = (Map<String, Object>) 
        JSON.deserializeUntyped(request.getBody());
        List<Object> sumVal = (List<Object>) results.get('Values');
        List<wrapVal> newLstValues = new List<wrapVal>();

        for (Object getValues : sumVal ) {
             wrapVal newLstValue = new wrapVal();
             newLstValue.nwValue = String.valueOf(getValues);
             newLstValues.add(newLstValue);
             System.debug("getValues::::::"+ newLstValue.nwValue);
       }
        return newLstValues;
  }

  public class wrapVal {
  public String nwValue { get; set; }
  }

调试: 在此处输入图像描述

所以我知道我做错了什么,如果可以与我分享一些建议或文档,那就太好了。谢谢

标签: apexsalesforce-lightninglwc

解决方案


似乎您需要使用@AuraEnabled 装饰类 wrapVal 的属性。否则 LWC 将不会收到它们。

 public class wrapVal {
  @AuraEnabled
  public String nwValue { get; set; }
  }

推荐阅读