Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: .post() requires callback functions but got a [object Undefined]

I'm new to node/express and I keep getting this exception.

Error: .post() requires callback functions but got a [object Undefined]

with this code

nu = require('./routes/create_newissue.js');
app.post('/create_newissue',nu.resources); 

The code in exports.create_newissue works fine if I put it in app.js. However, if I put it in a seperate .js file it throws the above error.

like image 862
RobD Avatar asked Apr 08 '14 16:04

RobD


2 Answers

You must have something like this in create_newissue.js

exports.resources = function(req, res){
   // Your code...
}
like image 115
Gilberto Avalos Avatar answered Nov 05 '22 12:11

Gilberto Avalos


The Error you've got indicates that the nu.resources you've sent to app.post( isn't a function.

I'm not sure what you've done, because you didn't give much of your code...

but this is the structure you need to have:

app.js: usually you put all the routes in a different file and add it to app.js like this:

 require('./routes')(app);

but it should also work if you do it direcly from app.js instead of routes.js

routes.js

var nu = require('./path/nu');
module.exports = function (app) {
          app.post('/create_newissue',nu.resourcesFunc);
    };

nu.js

exports.resourcesFunc = function (req, res) {
    //TODO: do your stuff here...
};

for summary, double check that you give a function (req, res) {...} to app.post() as it should be:

app.post('/address',function (req, res) {...});
like image 4
Aviram Netanel Avatar answered Nov 05 '22 12:11

Aviram Netanel