0

I am currently working on creating a prototype LWC that gives weather information for a given location. The location is being provided via user input. For some cities, it works fine - as in the case with San Francisco, Detroit, and others - but for other major cities - such as Chicago - I receive "undefined" in my data fields. When I hardcode the API with a given city, it works, as well as when I manually enter the api endpoint into my browser. In my first api call, I grab the lon and lat of the city by passing in the city name. Then I perform a second API call to get more detailed information by passing the lon and lat ascertained from the first API call.

Anyways, here's my apex class, can you please let me know if something is wrong with my parsing of the JSON received from the API call? Or possibly some other gremlin causing this to fail?

    global class WebServiceCallout 
    {
     
        @AuraEnabled (cacheable=true)
        global static WeatherData performCallout(String location)
        {
            location.trim();
    
            String api = 'https://api.openweathermap.org/data/2.5/weather?q='+location+',US&units=imperial&appid=yourAppId';
    
            HttpRequest req = new HttpRequest();
            req.setEndpoint(api);
            req.setMethod('GET');
            Http http = new Http();
            HTTPResponse res = http.send(req);
            JSONParser parser = JSON.createParser(res.getBody());
          
     
            WeatherData weather = new WeatherData();
            
            while (parser.nextToken() != null) 
            {
              if(parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                 parser.nextValue();
                if (parser.getCurrentName() == 'temp') 
                {
                    weather.cityTemp = parser.getDoubleValue();
                } 
                else if (parser.getCurrentName() == 'name') 
                {
                    weather.cityName = parser.getText();
                }
                else if (parser.getCurrentName() == 'humidity') 
                {
                    weather.cityHumidity = parser.getIntegerValue();
                }
                else if (parser.getCurrentName() == 'lat') 
                {
                    weather.cityLat = parser.getDecimalValue();
                }
                else if (parser.getCurrentName() == 'lon') 
                {
                    weather.cityLong = parser.getDecimalValue();
                }
                else if (parser.getCurrentName() == 'description') 
                {
                    weather.cityWeather = parser.getText();
                }
               }
            }
            Decimal lon = weather.cityLong;
            Decimal lat = weather.cityLat;
    
            String api2 = 'https://api.openweathermap.org/data/2.5/onecall?lat='+lat+'&lon='+lon+'&exclude=minutely,hourly&units=imperial&appid=yourAppId';
    
            HttpRequest req2 = new HttpRequest();
            req2.setEndpoint(api2);
            req2.setMethod('GET');
            Http http2 = new Http();
            HTTPResponse res2 = http.send(req2);
            JSONParser parser2 = JSON.createParser(res2.getBody());
    
            while (parser2.nextToken() != null) 
            {
                if (parser2.getCurrentToken() == JSONToken.FIELD_NAME) {
                    parser2.nextValue();
                    if (parser2.getCurrentName() == 'max') {
                        weather.cityDailyHigh = parser2.getDoubleValue();
                    } else if (parser2.getCurrentName() == 'min') {
                        weather.cityDailyLow = parser2.getDoubleValue();
                    } else if (parser2.getCurrentName() == 'rain') {
                        weather.cityDailyRain = parser2.getDoubleValue();
                    }
                }
            }
        return weather;
        } 
    
        global class WeatherData {
            @AuraEnabled public Double cityTemp;
            @AuraEnabled public String cityName;
            @AuraEnabled public Decimal cityLat;
            @AuraEnabled public Decimal cityLong;
            @AuraEnabled public Decimal cityHumidity;
            @AuraEnabled public String cityWeather;
            @AuraEnabled public Double cityDailyLow;
            @AuraEnabled public Double cityDailyHigh;
            @AuraEnabled public Double cityDailyRain;
        }
    }
6
  • 4
    P.S. You just posted the equivalent of a "password" on the open Internet. Please reset your API key as soon as possible to avoid abuse.
    – sfdcfox
    Jul 19 at 15:04
  • 1
    I can't believe I didn't catch that. Thank you. Jul 19 at 15:11
  • You're welcome!
    – sfdcfox
    Jul 19 at 15:17
  • 3
    Have you checked the response body directly in Apex? Try debugging that. It is possible it is returning multiple locations or something and the JSON format is different than what you are expecting with the other locations.
    – CyberJus
    Jul 19 at 16:03
  • 2
    Hello, and welcome to SFSE. Please take a moment to read How to Ask and take the tour. Have you looked at the Apex debug logs to verify the data you've sent and received? Please edit your post to include your findings. Jul 19 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.