簡體   English   中英

Node.js錯誤TypeError:對象不是回調函數

[英]Node.js error TypeError: object is not a function on callback

好的,..我一直在盯着這段代碼,我只是看不到我的代碼中的錯誤。 我的代碼可以正常連接到數據庫,控制台日志顯示的結果為

{ id: 7,
name: 'Tap\'s',
quicklist: 'Y',
message: 'Welcome..enjoy one of our many craft brews!',
fbflag: 'Y',
facebookurl: 'https://www.facebook.com/TapsPourhouseMooresville',
twflag: 'Y',
twitterurl: 'https://twitter.com/TapsPourhouse',
contactflag: 'Y',
contactemail: 'info@blazingpoint.com',
eventsflag: 'Y',
loyaltyflag: 'Y',
loyaltyclub: 'TAPped In',
loyaltymessage: 'Become a member of the TAPped In Club in order to receive special offers and to stay informed on upcoming events and exclusive offers!',
locdescription: 'Table 10' }

這就是我想要返回的內容,但是我一直在收到TypeError:object不是一個函數,它引用了下面注釋的行。

exports.get_site_setup = function (callback) {
var dbc;

async.waterfall([
    // get a connection
    function (cb) {
//           if (!name)
//               cb(backhelp.missing_data("site name"));
//           else
              db.db(cb);
    },

    function (dbclient, cb) {
        dbc = dbclient;
        dbc.query("select s.ID as id, s.NAME as name, s.QUICK_LIST_ENABLED as quicklist, s.MESSAGE as message, s.FBFLAG as fbflag, "+
                "s.FACEBOOKURL as facebookurl, s.TWFLAG as twflag, s.TWITTERURL as twitterurl, "+
                "s.CONTACTFLAG as contactflag, s.CONTACTEMAIL as contactemail, s.EVENTSFLAG as eventsflag, "+
                "s.LOYALTYFLAG as loyaltyflag, s.LOYALTYCLUB as loyaltyclub, s.LOYALTYMESSAGE as loyaltymessage, l.LOCDESC locdescription "+
                "from COMPANIES as c "+
                "left join SITES as s on c.ID = s.COMPANY_ID "+
                "left join LOCATION as l on s.ID = l.SITE_ID "+
                "where c.ID = 7 and s.ID = 7 and l.ID = 8",
                cb);
    }

],
function (err, results) {
    if (dbc) {dbc.end();}
    if (err) {
       callback(err);
    } else if (!results || results.length === 0) {
       // callback(backhelp.no_such_site());
    } else {
        callback(null, results[0]); //  <-- Error on this line
        console.log(results[0]);
    }
});
};

如果查看瀑布文檔,您會發現按數組的順序調用了回調,並且一旦處理完數組的所有元素,就會調用最終函數(err,結果)。

https://github.com/caolan/async#waterfall

所以在您的第一個回調中

 function (cb) {
//           if (!name)
//               cb(backhelp.missing_data("site name"));
//           else
              db.db(cb);
    },

當您參考db.db(cb); 該函數調用cb(null,dbclient,cb)轉到數組中指定的下一個函數嗎?

然后,在發生dbc.query的第二個函數中,您需要確保查詢調用cb(null,預期結果);

null表示瀑布沒有錯誤,然后繼續。

此外,您的結果變量可能是字符串,而不是數組。 請檢查一下。 因為如果是字符串,此測試將通過(可能是您的問題。

} else if (!results || results.length === 0) {

暫無
暫無

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

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