簡體   English   中英

TypeError:cb不是函數 - 帶回調

[英]TypeError: cb is not a function - with 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);

理由強調:

我們可以像變量一樣傳遞函數並將它們返回到函數中並在其他函數中使用它們。 當我們將回調函數作為參數傳遞給另一個函數時, 我們只傳遞函數定義 我們沒有在參數中執行該函數。 換句話說,我們沒有像執行函數時那樣使用尾對執行括號()傳遞函數。

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM