Getting JSON from a Function in javascript -


so lot of code, matter on line 22-25, , line 87-91. rest of code works. have nested function , want return json string. using console.log can tell running not return json string. part says //---------this part-------. there 2 parts asking about.

exports.post = function(request, response) {      var mssql = request.service.mssql;            //var data = '{"userid":"ryan3030@vt.edu1"}';     var inputjson = request.body.jsontext;     var json = json.parse(inputjson);     var userid = json.userid;      mssql.query("exec getmeetinginfo ?", [userid],       {         success: function(results3) {                 var meetingstoday = results3.length;              var meetingid = results3[0].meetingid;             var meetingname = results3[0].meetingname;             var meetingdescription = results3[0].meetingdescription;             var meetinglength = results3[0].meetinglength;             var meetingnotes = results3[0].meetingnotes;             var hostuserid = results3[0].hostuserid;  //--------------------------------------this part------------------------------               var jsonfinal = allinfofunction(mssql, meetingid, userid, meetingname, meetingdescription, meetinglength, meetingnotes, hostuserid, meetingstoday);                            console.log(jsonfinal);//does not work       response.send(statuscodes.ok, jsonfinal);//does not work //---------------------------------between these----------------------------------                       },       error: function(err) {             console.log("error is: " + err);             response.send(statuscodes.ok, { message : err });       }     });  };  function allinfofunction(mssql, meetingid, userid, meetingname, meetingdescription, meetinglength, meetingnotes, hostuserid, meetingstoday){     mssql.query("exec getlocation ?", [meetingid],                   { success: function(results2) {                             var meetinglocation = results2[0].meetinglocation;                             var jsonlocation = {"meetinglocation": meetinglocation};                                  mssql.query("exec getdatetime ?", [meetingid],                                   { success: function(results1) {                                             var length = results1.length;                                             var datetime = [];                                             datetime[0] = results1[0].meetingdatetime;                                             (var x= 1; x < length; x++) {                                                 datetime[x] =  results1[x].meetingdatetime;                                             }                                             //console.log(datetime);                                                  mssql.query("exec getdatetimevote",                                                   { success: function(results) {                                                         //console.log(results);                                                         var jsonoutput2 = {};                                                         var jsontemp = [];                                                         var length2 = results.length;                                                          for(var j = 0; j < length; j++){                                                             var vote = false;                                                             var counter = 0;                                                             for(var z = 0; z < length2; z++){                                                                 var = new date(results[z].meetingdatetime);                                                                 var b = new date(results1[j].meetingdatetime);                                                                 if(a.gettime() === b.gettime()){                                                                     counter = counter + 1;                                                                 }                                                                 if((a.gettime() === b.gettime()) && (results[z].userid == userid)){                                                                     vote = true;                                                                 }                                                             }                                                             var meetingtimeinput = {"time": b, "numvotes": counter, "vote": vote}                                                             jsontemp.push(meetingtimeinput);                                                             jsonoutput2.meetingtime = jsontemp;                                                          }                                                          var jsonfinal = {};                                                         var maininfoarray = [];                                                          var jsonmaininfo = {meetingid: meetingid, meetingname: meetingname, meetingdescription: meetingdescription, meetinglength: meetinglength, meetingnotes: meetingnotes, hostuserid: hostuserid, meetinglocation: meetinglocation };                                                         jsonmaininfo.meetingtime = jsontemp;                                                          jsonfinal.nummeetingstoday = meetingstoday;                                                         maininfoarray.push(jsonmaininfo);                                                         jsonfinal.meetinglist = maininfoarray;                                                         //response.send(statuscodes.ok, jsonfinal);  //---------------------------------------and part-------------------------------                                                                                                                 console.log(json.stringify(jsonfinal));//this outputs correct thing                                                         var lastone = json.stringify(jsonfinal);                                                         return lastone; //ths dosent work  //-------------------------------------between these-----------------------------------                                                  },                                                   error: function(err) {                                                         console.log("error is: " + err);                                                         //response.send(statuscodes.ok, { message : err });                                                   }                                                 });                                        },                                   error: function(err) {                                         console.log("error is: " + err);                                         //response.send(statuscodes.ok, { message : err });                                   }                                 });                           },                   error: function(err) {                         console.log("error is: " + err);                         //response.send(statuscodes.ok, { message : err });                   }                 }); } 

i've done little refactoring code take more modular approach. becomes quite tricky debug have above. here is:

exports.post = function(request, response) {     var mssql = request.service.mssql;            var inputjson = request.body.jsontext;     var json = json.parse(inputjson);     var userid = json.userid;      var jsonfinal = {};      getmeetinginfo(userid);      function getmeetinginfo(userid){             mssql.query("exec getmeetinginfo ?", [userid], {             success: function(results){                 jsonfinal.meetingstoday = results.length;                 jsonfinal.meetingid = results[0].meetingid;                 jsonfinal.meetingname = results[0].meetingname;                 jsonfinal.meetingdescription = results[0].meetingdescription;                 jsonfinal.meetinglength = results[0].meetinglength;                 jsonfinal.meetingnotes = results[0].meetingnotes;                 jsonfinal.hostuserid = results[0].hostuserid;                  // call next function                 getlocation(jsonfinal);             },             error: function(err) {                 console.log("error is: " + err);                 response.send(statuscodes.ok, { message : err });             }         });     }      function getlocation(){         mssql.query("exec getlocation ?", [jsonfinal.meetingid], {              success: function(results) {                 jsonfinal.meetinglocation = results[0].meetinglocation;                  // call next function                 getdatetime(jsonfinal);             },             error: function(err) {                 console.log("error is: " + err);             }         });          }      function getdatetime(){         mssql.query("exec getdatetime ?", [jsonfinal.meetingid], {              success: function(results) {                 var length = results.length;                 var datetime = [];                 (var x= 0; x < length; x++) {                     datetime[x] =  results[x].meetingdatetime;                 }                  // call next function                 getdatetimevote(datetime);             },             error: function(err){                 console.log("error is: " + err);                 }         });                                       }      function getdatetimevote(datetime){         mssql.query("exec getdatetimevote", {              success: function(results) {                 var jsontemp = [];                 var length2 = results.length;                   for(var j = 0; j < datetime.length; j++){                     var vote = false;                     var counter = 0;                     for(var z = 0; z < length2; z++){                         var = new date(results[z].meetingdatetime);                         var b = new date(results1[j].meetingdatetime);                         if(a.gettime() === b.gettime()){                             counter = counter + 1;                         }                         if((a.gettime() === b.gettime()) && (results[z].userid == userid)){                             vote = true;                         }                     }                     var meetingtimeinput = {"time": b, "numvotes": counter, "vote": vote}                     jsontemp.push(meetingtimeinput);                 }                  var jsonmaininfo = {                     meetingid: jsonfinal.meetingid,                      meetingname: jsonfinal.meetingname,                      meetingdescription: jsonfinal.meetingdescription,                      meetinglength: jsonfinal.meetinglength,                      meetingnotes: jsonfinal.meetingnotes,                      hostuserid: jsonfinal.hostuserid,                      meetinglocation: jsonfinal.meetinglocation                      meetingtime: jsontemp                 };                  var jsonfinal = {                     nummeetingstoday: jsonfinal.meetingstoday,                     meetingslist: [jsonmaininfo]                 };                  // call next function                 sendresponse(json.stringify(jsonfinal));             },             error: function(err) {                 console.log("error is: " + err);             }         });        }      function sendresponse(data){         response.send(statuscodes.ok, data);     } }; 

it looks lot different functionality pretty same. key point if @ code each subsequent function executed after success of previous function. chains methods executed in order , key point of difference.

one thing note here similarity between your

allinfofunction(...

and my

getmeetinginfo(userid)

both of these return undefined more or less immediately, after mssql query sent server. because request fired asynchronously, , javascript continues run through code. after it's fired of asynchronous request, has nothing left in function, returns. there no return value specified returns nothing (or undefined if will).

so in all, code in question code return undefined , attempt send response before has happened. code fixes problem firing sendresponse after queries have been executed.

i refactored code in text editor , haven't run it, or checked errors, follow basic outline may not idea copy , paste without checking it's not broken


Popular posts from this blog