Thursday, 1 June 2017

NodeJS How does callback work with route/controllers?

I am trying to understand how you guys use callback, and why I keep getting

TypeError: callback is not a function

Ok this is my router:

// getPriceRouter.js
router.post('/getPrice', function(req, res) {
    priceController.getPrice(req, res);
}   

And this is my controller:

// getPriceController.js
exports.getPrice = function(req, callback) {
    callback( { error:false, data:"HELLO" } );
}

This keeps giving me the error callback is not a function

(does this has something to do with export.?)

So I tried this:

exports.getPrice = function(req, callback(res)) {
    callback( { error:false, data:"HELLO" } );
}

And I tried this:

exports.getPrice = function(req, res) {
    return function (callback) {
        callback( { error:false, data:"HELLO" } );
    }
}

I think I am just not understanding callback correctly. Can anyone show me how to do this the right way?



via torbenrudgaard

No comments:

Post a Comment