我正在尝试从数据库中获取用户列表,完成后我想列出这些用户。我曾尝试使用回调,但得到的错误是 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.
关于javascript - 类型错误 : cb is not a function - with callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981900/