callback returns Uncaught TypeError: callback is not a function

Viewed 633

Background info

I am making an application which fetches data from the data via an AJAX call. If the success method meets the requirements, it calls a function and passes the callback parameter along as an argument. The callback gets invoked in a for loop then.

Problem

Whenever I invoke the callback function, it throws the error Uncaught TypeError: callback is not a function (on line 39)

Sources I have looked up and tried

Let me start off by saying that I have already done a console.log() on the callback parameter and the value is correct.

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

Uncaught TypeError: callback is not a function

add callback to function(e) Uncaught TypeError: callback is not a function

Uncaught TypeError: callback is not a function

My code

function Query(eventObject, button, callback)
{
        //replaced not relevant code here with a comment.
        success: function(response) 
        {
            if (button === "getB")
            {
                //success fetches correctly. Already checked whether response could be empty. This is not the case
                response = JSON.parse(response);
                if (callback)
                {
                    //pass the callback along
                    rollOut(response, callback);
                }
            }
        }
}
//THE FUNCTION I AM TRYING TO INVOKE
function setCheckboxes(valueToCompare)
{
    console.log(valueToCompare);
}

function rollOut(jsonResponse, callback)
{
    $(`.show-divSet`).on(`click`, function () {
        for (let i = 0; i < jsonResponse.length; i++)
        {
            if (jsonResponse[i]["version"] !== "")
            {
                //CONSOLE SHOWS ME THE CORRECT VALUE
                console.log(callback);
                //THIS IS LINE 39. THIS IS WHERE THE ERROR IS THROWN
                callback(jsonResponse[i]["version"]);
            }
        }
    });
}

How I instantiate my code all

$(function () {
    // I have already tried setCheckboxes WITHOUT quotes, but unfortunately this didn't work either
    Query("", "getB", "setCheckboxes");
});
0 Answers
Related