I know this is a Functions 101 type question, but we all need to learn sometime.
I have a AJAX call function (snippet below) stored in my own library .js file which returns a number of variables in an object.
function getMyDetails(){
//does processing
return {
firstName: firstName,
office: office,
manager: manager,
workphone: workphone,
isAdministrator: isAdministrator
}
}
In the script on my webpage I am calling this function as follows:
$userDetails = getMyDetails();
and then attempting to refer to the items in the object as such:
$userFirstName = $userDetails.firstName;
but when running the script I am being returned with a "$userDetails is undefined" error in the console.
Q: How should I be referencing the getMyDetails() function to enable me to expose the returned variables in my page?
Update:
My full function code is here. In an attempt to abstract the problem, I didn't include it earlier as I'm working on a SharePoint site and making use of the SPServices library and so my AJAX call is wrapped up in a separate library function
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;
}
}
Whilst I acknowledge it is [generally] not good practice, I am making the AJAX call synchronously (with the 'async: false' option) and all variables are being populated correctly in my alert statement before any further down-stream processing [appears to] take place.