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.
You must have something like this in create_newissue.js
exports.resources = function(req, res){
// Your code...
}
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) {...});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With