0

Ok, Rory pointed me in the callback direction for my question posted yesterday. I tried several different options, and at least got to the JavaScript variable from undefined to [object] [object], which is posted here to getCallBack.toString(), which doesn't work either. So My question is, I am trying to use session scope in ColdFusion to pre-assign select values using AJAX async calls. The values are displayed in console correctly, but attempting to assign the value to a JavaScript variable returns [object] [object]. Can someone review this and tell me what I am doing wrong?

here is the callback function;

function getCallBack(data) {
   // console.table(data);
    console.log('callback handler: ' + data);
    return data;
}

here is the async call;

jgetValue = function(a) {     
     return $.ajax({
            url: "cfc/SessionMgr.cfc",
            type: "get",
            //async: false,
            dataType: "json",
            data: {
                method: "jgetValue",
                variablename: a
            },
          error: function(msg) {
              console.log(msg);
          }
      }); 
 }

and the variable I am trying to set, either with/without the .toString() returns the same value;

var sel = jgetValue('ReportsShowAll').done(getCallBack.toString());

so I revisited the code, with Alex's incredibly helpful comment and have rewritten it to this;

function getCallBack(data) {
    console.log('callback handler: ' + data);
    return data;
}

jgetValue = function(a) {  
     return $.ajax({
                url: "cfc/SessionMgr.cfc",
                type: "get",
                dataType: "json",
                data: {
                    method: "jgetValue",
                    variablename: a
                },
              error: function(msg) {
                  console.log(msg);
              }
          }); 
 }


getter = function(a) {
    jgetValue(a).done(getCallBack);
}

console.log('getter for ReportsShowAll: ' + getter('ReportsShowAll'));

and the console writes this in the following order, it seems the 'getter' is still finishing before the json call is;

getter for ReportsShowAll: undefined 

callback handler: 1

anyone with an intelligent answer why this would happen? any help would be appreciated.

BigBear
  • 59
  • 4
  • https://stackoverflow.com/questions/4750225/what-does-object-object-mean – SOS May 29 '19 at 18:10
  • `.done()` expects a function (this is a callback handler), but you are passing a string. Try `.done(getCallBack)` for starters. Yet it would still not do what you expect. Read on on callback functions in JavaScript. You are lacking basics, it seems. – Alex May 31 '19 at 19:02
  • yes, I am lacking basics, and your response was all but useless, tx Alex. – BigBear Jun 04 '19 at 17:14

0 Answers0