javascript - 类型错误 : cb is not a function - with callback

标签 javascript mysql node.js callback

我正在尝试从数据库中获取用户列表,完成后我想列出这些用户。我曾尝试使用回调,但得到的错误是 TypeError: cb is not a function

var getAllUsers = function(users) {
    console.log(users)
}

function checkForUsers(table, cb) {
    connection.query('SELECT * from ' + table, function(err, rows, fields) {
        if(err) console.log(err);
        for(var i = 0; i < rows.length; i++) {
            users.push({id: id});   
            if(i == (rows.length - 1)) {
                cb(users)
            } 
        }
    });
}

checkForUsers('users',getAllUsers(users));

最佳答案

代替:

checkForUsers('users',getAllUsers(users));

使用:

checkForUsers('users',getAllUsers);

强调的原因:

We can pass functions around like variables and return them in functions and use them in other functions. When we pass a callback function as an argument to another function, we are only passing the function definition. We are not executing the function in the parameter. In other words, we aren’t passing the function with the trailing pair of executing parenthesis () like we do when we are executing a function.

Source

关于javascript - 类型错误 : cb is not a function - with callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981900/

相关文章:

mysql - 在 MySQL 中使用 CASE 将值设置为 NULL 或空白

Mysql RAND() 函数基于其他一些值

javascript - 从另一个函数停止函数执行

javascript - Svelte 3 getContext : Doesn't Get Store in IE11

javascript - JavaScript : How to detect a malformed URI?

java - 如何使控件在 javascript 中等待,直到对 applet 的调用得到响应,这在 JRE1.6V 中理想工作?

mysql - 需要第三个依赖项的包中的 npm 解析问题

javascript - 如何在 HTML 链接中使用 JavaScript 变量

mysql - 获取MySQL中小数范围的频率分布

node.js - 如何修复 travis-ci : FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 中的以下构建错误