-2

I am trying to use a callback function to place data into an array. When i try to call my callback function 'myCallback' i get an error saying TypeError: callbackFunction is not a function. Here is an example of what the code looks like.

var content = [];

var function1 = function(){
  var function2 = function(){
    query = function(){
      //CAML code
    }
    success = function(callbackFunction){
      callbackFunction("text");// TypeError: callbackFunction is not a function
    }
    failure = function(){
      //Error code
    }
  }
  function2();
  runQuery(one, query, success, failure, two);//Main function
}

function1();

function callbackfunction(data){
  content.push(data)
}

I am following THIS Stack Overflow answer. Maybe I am getting something confused? Not really sure why it is throwing me the error when it is clearly defined in the code.

Thanks pals.

Community
  • 1
  • 1
IE5Master
  • 413
  • 1
  • 3
  • 14

3 Answers3

1

You redefined the variable, you need to pass it in.

var content = [];

var function1 = function(callbackfunction){
  var function2 = function(){
    query = function(){
      //CAML code
    }
    success = function(){
      callbackFunction("text");// TypeError: callbackFunction is not a function
    }
    failure = function(){
      //Error code
    }
  }
  function2();
  runQuery(one, query, success, failure, two);//Main function
}

function1(callbackfunction);

function callbackfunction(data){  
  content.push(data)
}

And if that is your actual code, you will have issues with global variables if you make more than one call.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

The variable named callbackFunction passes to the success function has any type but a function and can't be called.

Dimava
  • 7,654
  • 1
  • 9
  • 24
0

You code is not very complete, but I could try to guess that problem is fllowing. You are trying to call a scoped method from inside the success callback, but you overshaddow its declaration by local variable.

success = function(callbackFunction){
  callbackFunction("text");// TypeError: callbackFunction is not a function
}

Try like this:

success = function(){
  callbackFunction("text");// TypeError: callbackFunction is not a function
}

And also move the declaration of the callback to be at the beginning of your code for better readability.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24