function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe SpinelliJoe Spinelli 

Lightning Web Component not showing API call results

Hello!

I'm working with a pretty basic Lightning Web Component. I've followed this guide loosely. https://trailhead.salesforce.com/en/content/learn/projects/lwc-build-flexible-apps/record-list 

The only differentiation is that I'm using a different API. 
Here is my js file
import { LightningElement, wire } from 'lwc';
import getStarships from '@salesforce/apex/StarshipController.getStarships';

export default class StarshipViewer extends LightningElement {
    starships;
	error;

    connectedCallback(){
        getStarships()
			.then(result => {
				this.starships = result;
			})
			.catch(error => {
				this.error = error;
			});
    }
}
Here is my controller
public inherited sharing class StarshipController {
    @AuraEnabled
    public static List<Starship> getStarships(){
        List<Starship> starships = new List<Starship>();
        string strEndPointURL = 'https://swapi.dev/api/starships/';
                
        starships = getStarships(strEndPointURL);
        system.debug(starships.size());
        return starships;
    }
    
    private static List<Starship> getStarships(string strEndPointURL){
        String strResponse = null;
        if(String.isNotBlank(strEndPointURL)) {
            HttpRequest request = new HttpRequest();
			request.setEndpoint(strEndPointURL);
			request.setMethod('GET');
			request.setHeader('Accept', 'application/json');
            
            try {  
                Http http = new Http();   
				HttpResponse response = http.send(request);
                                
                if (response.getStatusCode() == 200 ) {  
                    strResponse = response.getBody();  
                } 
                else {  
                    throw new CalloutException(response.getBody());  
                }   
            } 
            catch(Exception ex) {  
                throw ex;  
            }  
        } 
        
        if(!String.isBlank(strResponse)) {
            JSON2Apex res = JSON2Apex.parse(strResponse); 
            return res.results;
        } else {
        	return new List<Starship>();
        }        
    }
}

I can run it in anonymous window and see results. I also see the aura log that proves it is being called. It returns 10 records, but when I view the results the objects returned are just blank objects.

I'm not sure why it is showing up as blank. Am I being hit by some kind of limit when data is being sent to the front end?

Showing objects in chrome debugger are empty
 
Best Answer chosen by Joe Spinelli
Joe SpinelliJoe Spinelli
I found it! The Class Starship needed to have it's public properties set to @AuraEnabled in order for the data to be sent from the apex controller

All Answers

Abdul KhatriAbdul Khatri
Please try adding the following in your @AuraEnabled annotation.

@AuraEnabled (cacheable=true)

 
Joe SpinelliJoe Spinelli
Hmm, that didn't seem to change the result. Something I tried was just sending back a single object in the list instead of the API results. However it yielded the same blank object.
Joe SpinelliJoe Spinelli
I found it! The Class Starship needed to have it's public properties set to @AuraEnabled in order for the data to be sent from the apex controller
This was selected as the best answer
Julian Alejandro Morales VegaJulian Alejandro Morales Vega
What is JSON2Apex? I need help to complete the same exercise please 
Joe SpinelliJoe Spinelli
@Julian - It is a custom class I created to use as a JSON parser. This was just a POC so it isn't flashy and likely doesn't follow all best practices.

public with sharing class JSON2Apex {

    public Integer count {get;set;} 
    public String next {get;set;} 
    public Object previous {get;set;} 
    public List<Starship> results {get;set;} 

    public JSON2Apex(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'count') {
                        count = parser.getIntegerValue();
                    } else if (text == 'next') {
                        next = parser.getText();
                    } else if (text == 'previous') {
                        previous = parser.readValueAs(Object.class);
                    } else if (text == 'results') {
                        results = arrayOfResults(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
                        //consumeObject(parser);
                    }
                }
            }
        }
    }
    
    private static List<Starship> arrayOfResults(System.JSONParser p) {
        List<Starship> res = new List<Starship>();
        if (p.getCurrentToken() == null) p.nextToken();
        while (p.nextToken() != System.JSONToken.END_ARRAY) {
            res.add(new Starship(p));
        }
        return res;
    }
    
    public static JSON2Apex parse(String json) {
        System.JSONParser parser = System.JSON.createParser(json);
        return new JSON2Apex(parser);
    }    
}
Julian Alejandro Morales VegaJulian Alejandro Morales Vega
Thank you very much for the help: D it serves me a lot