GetUserProfileByName: Exposing variables returned as an object

Nov 1, 2013 at 1:14 AM
I put my hand up and admit this is more of a JavaScript Functions 101 question but am hoping that a member here will give me some enlightenment!

I have a function in a separate .js library which returns a number of user profile values as an object:
function getMyDetails(){
    var $userName = $().SPServices.SPGetCurrentUser({fieldName: "Name", debug: true });
    $().SPServices({
      operation: "GetUserProfileByName",
      async: false,
      AccountName: $userName,
      completefunc: function (xData, Status) {
        var firstName = getUPValue(xData.responseXML, "FirstName");
        var office = getUPValue(xData.responseXML, "Office");
        var manager = getUPValue(xData.responseXML, "Manager");
        var workphone = getUPValue(xData.responseXML, "WorkPhone");
        var SharepointAdministrator = getUPValue(xData.responseXML, "SharepointAdministrator");
        alert("What I've got is " + firstName +" and " + office +" and " +  manager +" and " + workphone  +" and " + SharepointAdministrator);
        
        return { 
            firstName: firstName,
            office: office, 
            manager: manager, 
            workphone: workphone, 
            SharepointAdministrator: SharepointAdministrator 
            }
       }
    });
    
    function getUPValue(x, p) {
      var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
        return $(this).find("Name").text() == p;
      }).find("Values").text();
      return thisValue;
    }
}
Within my custom page, I have a separate script which attempts to call the function as so:
var $userDetails = getMyDetails();
and then I attempt to expose the returned variables as such:
$userFirstName = $userDetails.firstName;
I've debugged the scripts and am happy that variables are being populated, but when I try to reference them using the $userDetails.firstName syntax I get a console error saying that $userDetails is undefined.

Would a kind soul please shed some light on the error of my ways?

Thanks
Coordinator
Nov 1, 2013 at 3:13 AM
I think Christophe's answer over on StackExchange should get you there:
http://stackoverflow.com/questions/19719244/exposing-variables-returned-as-an-array-by-function/19719663

M.
Marked as answer by fairfieldbusserv on 11/1/2013 at 2:06 PM
Nov 1, 2013 at 3:29 AM
Darn! Caught out pimping my questions on multiple fora!

Thanks Marc. I'll check it out.