0

I am returning an object in function but it returns undefined why? here is my code

parseXML = function (xml) {
        return {
            lon  : $(xml).find('GEOCODE').find("LOC").attr('lon'),
            lat  : $(xml).find('GEOCODE').find("LOC").attr('lat'),
            x    : $(xml).find('GEOCODE').find("LOC").attr('x'),
            y    : $(xml).find('GEOCODE').find("LOC").attr('y')
        };
    },

    searchRoute = function () {
        var userloc, dest; 
        userloc = cityattr.init(from, to, fromurl, parseXML);
        //dest    = cityattr.init(from, to, fromurl, parseXML);

        console.log(userloc);
    };

Update

When I do console.log() inside parseXML then it returns correct object

parseXML = function (xml) {
        var obj = {
            lon  : $(xml).find('GEOCODE').find("LOC").attr('lon'),
            lat  : $(xml).find('GEOCODE').find("LOC").attr('lat'),
            x    : $(xml).find('GEOCODE').find("LOC").attr('x'),
            y    : $(xml).find('GEOCODE').find("LOC").attr('y')
        };

        console.log(obj);
    },

Update 2

still return undefined

parseXML = function (xml) {
        return {
            lon  : $(xml).find('GEOCODE').find("LOC").attr('lon'),
            lat  : $(xml).find('GEOCODE').find("LOC").attr('lat'),
            x    : $(xml).find('GEOCODE').find("LOC").attr('x'),
            y    : $(xml).find('GEOCODE').find("LOC").attr('y')
        };
    },

here is code for cityattr

var Xml = function () {
var to, from, url, result,

    init = function (fromaddress, toaddress, link, callback) {
        from    = fromaddress;
        to      = toaddress;
        url     = link;

        requestXml(callback);
    },

    requestXml = function (callback) {
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            success: callback
        });
    },

    getResult = function () {
        return result;
    };

 return {
    init        : init,
    getResult   : getResult
 };
};
Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • possible duplicate of [How to return AJAX response Text?](http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text) – Quentin Nov 08 '12 at 12:17

1 Answers1

4

You define the function using a function expression (so it isn't hoisted) after you pass parseXML to cityattr.init.

At the time you pass it, it hasn't yet been defined.

Change the order of your code or use a function declaration.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • OP probably wants to call the function as well – John Dvorak Nov 08 '12 at 11:56
  • Possibly. Without knowing what `cityattr.init` does, it is hard to say. It might be expecting a callback function. – Quentin Nov 08 '12 at 11:58
  • Well I changed the order but still it returns `undefined` – Om3ga Nov 08 '12 at 11:59
  • Judging from "I am returning an object in function but it returns `undefined`", OP seems to think the function is being called – John Dvorak Nov 08 '12 at 12:00
  • @JanDvorak What to do now? I think I am already calling a function which is parseXML – Om3ga Nov 08 '12 at 12:02
  • You aren't returning anything from `parseXML` now. – Quentin Nov 08 '12 at 12:08
  • `console.log(userloc);` is testing the return value of `cityattr.init` not `parseXML`. You still haven't shared `cityattr.init` with us. – Quentin Nov 08 '12 at 12:10
  • When I return after changing the order of functions then still it returns undefined but when I do console.log() inside parseXML then it does logs out correct object. Hope this clarifies. – Om3ga Nov 08 '12 at 12:12
  • `console.log(userloc);` is still testing the return value of `cityattr.init` not `parseXML`. You still haven't shared `cityattr.init` with us. Your updated `parseXML` has no `return` statement now so it will always return `undefined` – Quentin Nov 08 '12 at 12:13
  • `console.log(userloc);` is still testing the return value of **`cityattr.init`** (which we **still** can't see) not `parseXML`. – Quentin Nov 08 '12 at 12:15
  • @x4f4r — See http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text — You're using Ajax, you can't return a value from it. – Quentin Nov 08 '12 at 12:17
  • Is there any technique for this? – Om3ga Nov 08 '12 at 12:19
  • @x4f4r — See the accepted answer on http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text – Quentin Nov 08 '12 at 12:20
  • @Quentin can I use $.get() method over here. I mean can I return data from $.get() method? That accepted answer will not fullfil my requirements here. – Om3ga Nov 08 '12 at 12:31
  • Not in any sane way. If you are doing Ajax, then do work on data using a callback, not a return value. – Quentin Nov 08 '12 at 12:42