Reputation: 455
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.
Upvotes: 9
Views: 45591
Reputation: 777
Check is there is typo error while importing your functions. I had the same issue on checking I have done some spelling mistake.
Upvotes: 0
Reputation: 71
problem with importing the file. In your case you missed parenthesis It should be nu = require('./routes/create_newissue.js')();
Upvotes: 0
Reputation: 13625
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) {...});
Upvotes: 4
Reputation: 512
You must have something like this in create_newissue.js
exports.resources = function(req, res){
// Your code...
}
Upvotes: 16