2017-02-01 7 views
0

データベースからユーザーのリストを取得しようとしていますが、これが完了したらこれらのユーザーをリストします。私は、コールバックを使用しようとしましたが、TypeError: cb is not a functionTypeError:cbが関数ではありません - コールバック付き

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)); 
+1

try:checkForUsers( 'users'、getAllUsers); –

+0

ああ、それはうまくいく –

+0

答えとして追加しても大丈夫でしょうか? –

答えて

0

の代わりにエラーを取得していること:

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

関連する問題