0

I am new to Lightning Components. 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 greeted with an 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

({
  ...    
    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

1 Answer 1

3

I suspect it has something to do with data not existing before you call it. It seems like you have a bunch of stuff mixed up.

In this function:

    doLayout: function(response, component) {
      var warning = component.find('warning');
      if (data.error) {      <-------------------------HERE! data does not exist 
        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()); <---THIS LINE SHOULD COME BEFORE YOU CALL data ABOVE
      component.set("v.restaurantList", data.bizArray);
      console.log("The Data: ", data);
  }
})

I'm being a deliberately vague because I am hoping you can take it from here. Long story short, you have your code in the wrong order.

That is the Restaurant Locator project, IIRC. You might want to read through the instructions again, and check where you got things out of order to correct your problem.

If you do that and are still stuck, write a comment, and I don't mind being more specific.

1
  • Hey @pchittum actually i completed this badge successfully few days back.But now when i open contact or account page i am getting this error.
    – Pavan tej
    Commented Jun 12, 2017 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .