SPServices GetListItems return value.

Aug 10 at 1:04 PM
Hi,

I'm trying to get a username using GetListItems and passing in an ID to a function. The function seems to work and if I do an alert within the "completefunc:" then I get the username I'm looking for, but I can't seem to access it anywhere else. It just returns as undefined. Am I doing something mental or am I just misunderstanding how this all works? Here is my code:
function getUserInfo(userId)
        {
          
          $().SPServices({
             operation: "GetListItems",
             listName: "User Information List",
             CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + userId + "</Value></Eq></Where></Query>",
             CAMLRowLimit: 1,
             completefunc: function (xData, Status) {
                 var userName;
                 var row = $(xData.responseXML).SPFilterNode("z:row").get(0);
                 userName = $(row).attr("ows_Name");
                 alert(userName); //This alerts the username as expected.
             }           
          });
          alert (userName); //This alerts "undefined".
        }
I thought it would be as simple as using a "return userName" where the second alert is. But as I said, that just comes back as "undefined".

Any help would be greatly appreciated.

Cheers...

John
Coordinator
Aug 12 at 3:41 PM
John:

You've got a scoping issue with your userName variable. You define it inside the function and then alert it outside the function - thus it's undefined.

M.