0

begginer in Javascript and Node Js here. While trying to do my first, simple update function, i got the error :

TypeError: callback is not a function.

I searched for the answer online but this problem is still a mistery.

function UpdateProductsCodes(columns, returnColumns, type, callback) {

    for (var i = 0; i < columns.ids.length; i++) {

        updateSql = "UPDATE TProductCodes SET code =?, product_id =? OUTPUT inserted.id, inserted.code, inserted.product_id INTO #returnValues WHERE ids =?";
        var params = [];

        params.push(columns.codes[i]);
        params.push(columns.product_ids[i]);
        params.push(columns.ids[i]);

        sql.query(conn_str, updateSql, params, function (err, products, more) {
            //Code stops here
            //TypeError: callback is not a function

            if (err) {
                callback(err, null);
                return;
            };
            if (!more) {
                callback(null, products);
            }
        });
    }
}

This function should do a simple update, nothing more. Its used here:

UpdateProductsCodes(req.body.entities, conditions, returnColumns, type, function (err, products) {
if (err) {
    console.dir(err);
    res.writeHead(500, { 'Content-Type': 'application/json' });
    res.write(JSON.stringify(utils.GenerateResponse(err.message, true, 'JSON')));
    res.end();
    return;
}

res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(utils.GenerateResponse(products, false, type)));
res.end();
});
4
  • 3
    can you show how you are calling "UpdateProductsCodes"? what are you passing through as the callback parameter? Commented Jun 10, 2019 at 11:09
  • If i use this: if (callback) { callback(err, null); } i get the erorr again : TypeError: callback is not a function Commented Jun 10, 2019 at 12:06
  • @CristiPriciu Show us how you are calling the UpdateProductCodes function. The error message is telling that whatever value was supplied for the callback option, that value is not an executable function. It might be something else like a string or a number or an object, but what it definitely isn't is a function. It needs to be a function. So take a look at how you are creating and providing that value. If you need help with it, show us the code which relates to that. Commented Jun 10, 2019 at 13:57
  • Thank you, i added to my initial post. Commented Jun 10, 2019 at 14:45

1 Answer 1

1

The problem is that you are simply sending the wrong number of arguments when you call the function.

The function accepts four inputs: columns, returnColumns, type, callback. But in your example, you are sending five inputs: req.body.entities, conditions, returnColumns, type, function (err, products)

The last one (the function, in this case) is therefore ignored. The value which the function is receiving as being the callback value is in fact the one you've named type when you call the function, because that's the fourth argument you provide. This value is not an executable function - which is what the error message is telling you.

Now I don't know which values are the ones you actually need/want to send to the function, but clearly one of them is redundant and you need to remove it from the calling code. Based purely on the names, I'd guess that one of either req.body.entities or conditions is not needed, but of course I can't see what those variables contain, and I can't be certain of your intent, so you'll have to work it out yourself.


P.S. I also note that your function never actually uses the returnColumns or type parameters which it receives, so you maybe should consider whether you actually need to accept these at all. Perhaps they can be removed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.