This project has moved and is read-only. For the latest updates, please go here.

SPServices GetListItems return value.

Aug 10, 2016 at 2:04 PM

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)
             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.


Aug 12, 2016 at 4:41 PM

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.