简体   繁体   English

回调不是Node JS中的更新功能

[英]Callback is not a function for update in Node JS

begginer in Javascript and Node Js here. 在这里用Javascript和Node Js开始。 While trying to do my first, simple update function, i got the error : 尝试执行我的第一个简单更新功能时,出现错误:

TypeError: callback is not a function. TypeError:回调不是函数。

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();
});

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 . 该函数接受四个输入: columns, returnColumns, type, callback But in your example, you are sending five inputs: req.body.entities, conditions, returnColumns, type, function (err, products) 但是在您的示例中,您将发送五个输入: 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. 实际上,该函数作为callback值接收的值就是您在调用该函数时已命名的type ,因为这是您提供的第四个参数。 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. 纯粹基于名称,我猜想req.body.entitiesconditions之一,但是我当然看不到这些变量包含什么,也无法确定您的意图,所以您必须自己解决。


PS 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. PS我还注意到,您的函数从不实际使用它接收到的returnColumnstype参数,因此您可能应该考虑是否真的需要接受这些参数。 Perhaps they can be removed. 也许可以将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM