简体   繁体   English

Node.js错误TypeError:对象不是回调函数

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

Ok guys..i've been staring at this code and i just don't see the error in my code. 好的,..我一直在盯着这段代码,我只是看不到我的代码中的错误。 My code is connecting to the DB fine and my console log is showing my results as 我的代码可以正常连接到数据库,控制台日志显示的结果为

{ 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' }

which is what i am wanting to get returned back but I keep getting TypeError: object is not a function referring to line commented below. 这就是我想要返回的内容,但是我一直在收到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]);
    }
});
};

If you review the documentation for waterfall you will see that the callbacks get called in the order of the array and the final function(err, results) is called once all elements of the array have been processed. 如果查看瀑布文档,您会发现按数组的顺序调用了回调,并且一旦处理完数组的所有元素,就会调用最终函数(err,结果)。

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

So in your first callback 所以在您的第一个回调中

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

When you reference db.db(cb); 当您参考db.db(cb); is that function calling cb( null, dbclient, cb) to go to the next function specified in your array? 该函数调用cb(null,dbclient,cb)转到数组中指定的下一个函数吗?

Then in the second function where dbc.query happens you will need to be sure the query calls cb( null, expected result); 然后,在发生dbc.query的第二个函数中,您需要确保查询调用cb(null,预期结果);

The null indicates to waterfall that there is no error and to continue. null表示瀑布没有错误,然后继续。

Also your results variable may be a string and not an array. 此外,您的结果变量可能是字符串,而不是数组。 Please check that. 请检查一下。 As this test would pass if it was a string (likely your problem. 因为如果是字符串,此测试将通过(可能是您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM