我目前正在使用Express + Node开发应用程序.我最近使用以下语法添加了一个.post到该app.js文件的新路由:
app.post('/api/posts/saveComment', posts.saveComment);
Run Code Online (Sandbox Code Playgroud)
posts 以上定义为:
var posts = require('./routes/posts.js');
Run Code Online (Sandbox Code Playgroud)
并saveComment定义如下:
exports.saveComment = function(req, res) {
//function stuff in here, yada yada
}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试运行应用程序时,节点发出错误:
Error: .post() requires a callback functions but got a [object Undefined]
Run Code Online (Sandbox Code Playgroud)
saveComment显然是一个功能,我不明白为什么它看不到这个.我上面定义了另一个函数saveComment,我能够完全正确地引用而没有错误,但是将函数内容复制到saveComment仍然会产生相同的错误.我很茫然,任何帮助都非常感激.
每个请求,内容 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();
});
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6803 次 |
| 最近记录: |