I'm currently working on an app using Express + Node. I recently added a new .post
route to the app.js
file, using the following syntax:
app.post('/api/posts/saveComment', posts.saveComment);
posts
is defined above as:
var posts = require('./routes/posts.js');
And saveComment
is defined as so:
exports.saveComment = function(req, res) {
//function stuff in here, yada yada
}
Now, node is throwing an error when I try to run the app:
Error: .post() requires a callback functions but got a [object Undefined]
saveComment
is clearly a function, I'm not understanding why it can't see this. I have another function defined right above saveComment
that I'm able to reference completely fine without error, however copying that functions contents to that of saveComment
still yields the same error. I'm at a loss, any help is much appreciated.
Per request, contents of posts.js
var mongo = require('../mongo.js');
exports.queryAll = function(req, res) {
var db = mongo.db;
db.collection('posts', function(err, collection) {
collection.find().toArray(function(err, doc) {
if (err)
res.send({error:err})
else
res.send(doc)
res.end();
});
});
}
exports.saveCommment = function(req, res) {
var db = mongo.db,
BSON = mongo.BSON,
name = req.body.name,
comment = req.body.comment,
id = req.body.id;
db.collection('posts', function(err, collection) {
collection.update({_id:new BSON.ObjectID(id)}, { $push: { comments: { poster:name, comment:comment }}}, function(err, result) {
if (err) {
console.log("ERROR: " + err);
res.send(err);
}
res.send({success:"success"});
res.end();
});
});
}
Well...embarrassing answer, saveCommment
is defined with 3 m's in my posts.js
. Ugh.
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