4

私は現在、Express + Node を使用したアプリに取り組んでいます。最近、次の構文を使用して.post、ファイルに新しいルートを追加しました。app.js

app.post('/api/posts/saveComment', posts.saveComment);

postsは上で次のように定義されています。

var posts = require('./routes/posts.js');

そしてsaveComment、次のように定義されています。

exports.saveComment = function(req, res) {
    //function stuff in here, yada yada
}

今、アプリを実行しようとするとノードがエラーをスローしています:

Error: .post() requires a callback functions but got a [object Undefined]

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();
        });
    });
}
4

3 に答える 3