2017-02-01 4 views
0

Ich versuche, eine Liste der Benutzer aus einer Datenbank zu erhalten, und wenn dies abgeschlossen ist, möchte ich diese Benutzer auflisten. Ich habe versucht, einen Rückruf zu verwenden, aber den Fehler bekommen, dass TypeError: cb is not a functionTypeError: cb ist keine Funktion - mit Rückruf

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

Versuchen Sie: checkForUsers ('users', getAllUsers); –

+0

ah ok, das funktioniert –

+0

Wäre es in Ordnung, wenn ich es als Antwort hinzufügen? –

Antwort

0

Statt:

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

Verwendung:

checkForUsers('users',getAllUsers); 

Der Grund betonte:

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