Skip to main content
1 of 2
Pavan tej
  • 1.4k
  • 5
  • 43
  • 92

Issue in trailhead challenge

I am new to lightning when i try to finish a trailhead challenge called Build a Restaurant component Bascially it show Restaurants near to opportunity billing address and in controller there are method to handle address in both contact as well as opportunity

But when i try to open contact page i am geeting error massage like.

Uncaught Error in $A.getCallback() [Cannot read property 'error' of undefined] Callback failed: apex://InTheArea/ACTION$getListByAddress

For reference:enter image description here

Component handler

({
 getLocalList: function(component, recID) {
    var spinner = component.find('spinner');
    $A.util.removeClass(spinner, "slds-hide");
    var objectType = component.get("v.sObjectName");      
    var searchTerm = component.find("searchTerm").get("v.value");
    if (searchTerm == null) {
        searchTerm = component.get("v.defaultSearch");
    }
    if (recID) {
        var action = component.get("c.getListByAddress");
        action.setParams({
            "recordId": recID,
            "objectType": objectType,
            "searchQuery": searchTerm
        });
    } else {
        var location = component.get("v.location");
        var action = component.get("c.getLocal");
        action.setParams({
            "searchTerm": searchTerm,
            "lat": location.coords.latitude,
            "lon": location.coords.longitude
        });
    }
    action.setCallback(this, function(response) {
    this.doLayout(response, component);
    });
    
    $A.enqueueAction(action);
},

    doLayout: function(response, component) {
        var warning = component.find('warning');
if (data.error) {
    component.set("v.errorMessage", data.error);            
    $A.util.removeClass(warning, 'slds-hide');
} else {
    $A.util.addClass(warning, 'slds-hide');
}

        var spinner = component.find('spinner');
        $A.util.addClass(spinner, "slds-hide");
        var data = JSON.parse(response.getReturnValue());
        component.set("v.restaurantList", data.bizArray);
        console.log("The Data: ", data);
    }
})

Apex controller

public class InTheArea {
@AuraEnabled
public static String getLocal (String searchTerm, Decimal lat, Decimal lon) {
  String url = 'https://th-yelp-locator.herokuapp.com/search?address=' + lat +','+ lon + '&term=' + EncodingUtil.urlEncode(searchTerm, 'UTF-8');
  String resultList = getHttp(url);
  return resultList;   
}

private static String getHttp (String url) {
  HttpRequest req = new HttpRequest();
  Http http = new Http();
  req.setMethod('GET');    
  req.setEndpoint(url);
  HTTPResponse res = http.send(req);
  return res.getBody();
}

@AuraEnabled
public static String getListByAddress(Id recordId, String objectType, String searchQuery) {
        try {
    String location;
    if (objectType == 'Account') {            
        location = accountSelect(recordId);
    } else if (objectType == 'Contact') {
        location = contactSelect(recordId);
    }    
     else {
    return '{"error": "This component cannot be used on this page type."}';
}
       
    String url = 'https://th-yelp-locator.herokuapp.com/search?address=' + EncodingUtil.urlEncode(location, 'UTF-8') + '&term=' + EncodingUtil.urlEncode(searchQuery, 'UTF-8');
    String resultList = getHttp(url);
        return resultList;
  }  catch(Exception ex){
      return '{"error": "' + ex.getMessage() + '"}';
  }
}

private static String contactSelect (Id recordId) {
  Contact contact = [SELECT MailingStreet, MailingCity, MailingState FROM Contact WHERE Id=:recordId];
  String location = contact.MailingStreet + ',' + contact.MailingCity + ',' + contact.MailingState;
  return location;
}

private static String accountSelect (Id recordId) {
  Account account = [SELECT BillingStreet, BillingCity, BillingState FROM Account WHERE Id=:recordId];
  String location = account.BillingStreet + ',' + account.BillingCity + ',' + account.BillingState;
  return location;
}
}

Please let me know root cause for the issue

Pavan tej
  • 1.4k
  • 5
  • 43
  • 92