0

The function getInfo() reads a JSON, based on the keys passed returns an URL by making simple concatenations. I see the method returns an URL but the calling method gets the value as undefined.

function getInfo(DebugTool, ServiceName, ELBOrInstance) {
if (DebugTool == "Kibana") {
        console.log("Kibana");
        if (ELBOrInstance == "ELB") {
            $.each( data.ServiceBlock, function( index, serviceblock ) {
                if (serviceblock.Service == serviceName) {
                    console.log("Returning " + encodeURI(data.KibanaELBLink + data.KibanaELBSuffix.replace("ELBNAME", serviceblock.ELBName)));
                    return encodeURI(data.KibanaELBLink + data.KibanaELBSuffix.replace("ELBNAME", serviceblock.ELBName));
                }
            });
        }
        else {
            return (data.KibanaInstanceLink + data.KibanaDaemonSuffix.replace("DAEMONNAME", serviceName));
        }
    }..........
}

console.log prints the right URL I need. I return the same thing to the calling method but the actual return value is undefined

console.log ("Getting " + getInfo("Kibana", key, "ELB"));

Banged my head for a bit, can't figure out.

Gopi
  • 216
  • 1
  • 3
  • 11
  • 2
    You cannot `return` out of a `$.each` callback. Use a proper `for … of` loop instead – Bergi Sep 27 '18 at 21:31
  • A good dupe target, anyone? – Bergi Sep 27 '18 at 21:36
  • 1
    Ugh this is painful, I wish I could. Here's a potential target https://stackoverflow.com/questions/11867150/why-is-function-return-value-undefined-when-returned-in-a-loop Thank you Bergi – Gopi Sep 27 '18 at 21:38

0 Answers0