javascript - Nested JQuery Function Returns 'undefined' or '[object Object]' -
hello friends on stackoverflow:
i struggling find causing nested jquery function return values of 'undefined' or '[object object]'. have commented location of first function, contains function call. can see research subject not alone in perplexing issue. depending on bit of code use inside nested jquery function inside second "getattributevalue" function different results. stackoverflow helpers can see various permutations of code have been trying, commented out debugging, etc. 'alert' method returns perfect clear data way want it. others return nothing, 'undefined', or '[object object]', never perfect clear data want. have been @ long enough know need help.
the data source web api / web service providing json data.
here code:
//# primary function gathers data web api / web service: $(document).ready(function () { $("#loaddata").click(function () { $.get(url, function (data) { var myhtmldatatable = "<table class='table table-striped table-hover'>"; myhtmldatatable = myhtmldatatable + "<tr><th>webid</th><th>attributes</th><th>values</th></tr>"; (i = 0; < data.items.length; i++) { myhtmldatatable = myhtmldatatable + "<tr>"; myhtmldatatable = myhtmldatatable + "<td class='col-sm-1'>" + (data.items[i].webid) + "</td>"; myhtmldatatable = myhtmldatatable + "<td class='col-sm-1'>" + (data.items[i].name) + "</td>"; myhtmldatatable = myhtmldatatable + "<td class='col-sm-1'>"+ getattributevalue(data.items[i].webid) + "</td>"; //~ function call here myhtmldatatable = myhtmldatatable + "</tr>"; } myhtmldatatable = myhtmldatatable + "</table>"; document.getelementbyid("mydata").innerhtml = myhtmldatatable; }); }); }); //# function returns current value , uom target attribute: function getattributevalue(attributewebid) { var theenvelopeplease = ""; //~ clear variable var atrburl = "https://<<the target web api root address>>"; var atrbprefix = "streams/"; var atrbwebid = attributewebid; var atrbextension = "/value"; //~ concatenate url used make request atrburl = atrburl + atrbprefix + atrbwebid + atrbextension; // theenvelopeplease = $.getjson(atrburl, function (data) { data.value }); return $.getjson(atrburl, function (data) { //return (data.value) + " " + (data.unitsabbreviation); //alert(data.value + " " + data.unitsabbreviation); //theenvelopeplease = theenvelopeplease + (data.value) + " " + (data.unitsabbreviation); (data.value) + " " + (data.unitsabbreviation); }); // return theenvelopeplease; } the json data being retrieved nested jquery function looks this:
{ "timestamp": "2015-06-03t22:22:00z", "value": 89.660293579101563, "unitsabbreviation": "%", "good": true, "questionable": false, "substituted": false } perhaps there jquery expert reviewing sees mistakes easily. appreciated.
update: because has been requested want show when use console.log: 
update: hi @ironflare: thank suggestion. tried suggestion revised function code follows:
function getattributevalue(attributewebid) { var theenvelopeplease; //~ clear variable var atrburl = "https://<<the target web api root address>>"; var atrbprefix = "streams/"; var atrbwebid = attributewebid; var atrbextension = "/value"; //~ concatonate url used make request atrburl = atrburl + atrbprefix + atrbwebid + atrbextension; $.getjson(atrburl, function (data) { theenvelopeplease = (data.value) + " " + (data.unitsabbreviation); }); return theenvelopeplease; } and resulting data 'undefined' (:-c)
=======================================================
hi @ironflare , thank suggestion. modified function follows:
function getattributevalue(attributewebid) { var theenvelopeplease; //~ clear variable var atrburl = "https://<<the target web api root address>>"; var atrbprefix = "streams/"; var atrbwebid = attributewebid; var atrbextension = "/value"; //~ concatenate url used make request atrburl = atrburl + atrbprefix + atrbwebid + atrbextension; $.getjson(atrburl, function (data) { theenvelopeplease = (data.value) + " " + (data.unitsabbreviation); console.log(data); }); return theenvelopeplease; } and here view of results: 
====================================================
update: following thread have attempted permutation:
//# stackoverflow permutation 03: function getattributevalue(attributewebid) { var theenvelopeplease; //~ clear variable var atrburl = "https://<<the target web api root address>>"; var atrbprefix = "streams/"; var atrbwebid = attributewebid; var atrbextension = "/value"; //~ concatenate url used make request atrburl = atrburl + atrbprefix + atrbwebid + atrbextension; theenvelopeplease = $.getjson(atrburl, function (data) { console.log(data.value) + " " + (data.unitsabbreviation); return (data.value) + " " + (data.unitsabbreviation); }); return theenvelopeplease; } and here view of results: 
the problem code, , reason why alert working when return doesn't, because $.getjson doesn't pass callback output main function. when line executed:
(data.value) + " " + (data.unitsabbreviation); it's running in context of callback, getattributevalue isn't receiving data. should able fix defining empty variable before running $.getjson, changing variable response receive, , returning variable, code shows started do. have close being correct. far can tell, need remove first return before $.getjson , uncomment both line starting //theenvelopeplease = , second return statement, // return theenvelopeplease;. should result in getattributevalue looking this:
function getattributevalue(attributewebid) { var theenvelopeplease; ... atrburl = atrburl + atrbprefix + atrbwebid + atrbextension; $.getjson(atrburl, function (data) { theenvelopeplease = (data.value) + " " + (data.unitsabbreviation); }); return theenvelopeplease; } hope helps!
Comments
Post a Comment