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
 };
};
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

15 Comments

OP probably wants to call the function as well
Possibly. Without knowing what cityattr.init does, it is hard to say. It might be expecting a callback function.
Well I changed the order but still it returns undefined
Judging from "I am returning an object in function but it returns undefined", OP seems to think the function is being called
@JanDvorak What to do now? I think I am already calling a function which is parseXML
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.