admin管理员组

文章数量:1188224

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.

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.

Share Improve this question edited May 3, 2019 at 4:53 Pavindu 3,1128 gold badges51 silver badges86 bronze badges asked Apr 8, 2014 at 16:34 RobDRobD 4554 gold badges11 silver badges25 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 16

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) {...});

problem with importing the file. In your case you missed parenthesis It should be nu = require('./routes/create_newissue.js')();

Check is there is typo error while importing your functions. I had the same issue on checking I have done some spelling mistake.

本文标签: javascriptError post() requires callback functions but got a object UndefinedStack Overflow