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.
4 Answers
Reset to default 16You 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
版权声明:本文标题:javascript - Error: .post() requires callback functions but got a [object Undefined] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738389056a2084333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论