2016-08-27 9 views
3

私はdbに新しい投稿を挿入しようとするとmongodbで基本的な処理を行いますが、Testは関数ではないので投稿者のメッセージを受け取ります。モデルは郵便配達員の関数エラーではありません

ルータの機能は次のとおりです。

router.route('/createtests').post(function (req, res, next) { 

    var Test = new Test(req.body); 
    postTest(Test, function (data) { 

     res.json({'message': 'The test created sucessfully'}); 

    }); 

}); 

var postTest = function(test, cb){ 

    Test.save(function(err,data){ 

     cb(data); 

    }); 

}; 

私のスキーマは次のとおりです。

var TestSchema = common.Schema({ 

title     : String, 
testCreator    : String, 
datePosted    : { 
          type: Date, 
          default: Date.now 
          }, 
totalQuestions   : Number, 
totalTime    : Number, 
numberOfPeopleTaking : Number, 
dateOfTest    : Date, 
marksPerQuestions  : Number, 
imageUrl    : String, 
testType    : String, 

}); 
var Test = common.conn.model('Test', TestSchema); 
console.log(typeof Test);// logging as function 
console.log(Test);// logging full model with schema 
module.exports = Test; 

Iamの応答を得るあなたの機能postTest

{ 
"message": "Test is not a function", 
"error": {} 
} 
+0

はmongooseと共通ですか? – winter

+0

はい。それはマングースの目的です。スキーマ:require( 'mongoose')。スキーマは共通のプロパティです。 –

+0

'var test = common.conn.model( 'Test'、TestSchema);' – winter

答えて

1

に従うように、あなたは 't' でtestを持っていて、 'T'(Test.save())で保存されます。大文字/小文字の入力ミス。これがあなたの問題の原因です。

var postTest = function(test, cb){ 

    test.save(function(err,data){ //see the change here 'test' instead of 'Test' 

     cb(data); 

    }); 

}; 

また、あなたがvariable名とmodel名の両方としてTestを使用しているcommon.conn.model

var Test = common.model('Test', TestSchema); 

common.modelにEDIT

を変更。 varをtestに変更します。あなたの問題を解決するはずです。

router.route('/createtests').post(function (req, res, next) { 

    var test = new Test(req.body); //See the change here. 'test' instead of 'Test' 
    postTest(test, function (data) { //pass 'test' 

     res.json({'message': 'The test created sucessfully'}); 

    }); 

}); 
+0

はい、それは意味があります....しかし、私はまだ同じエラー..... ..... –

+0

編集が動作しているかどうかチェックできますか? –

+0

私は何を意味するのか分かりません。 –

0

正しい方法でコードを記述する必要があります。

const Test = require('../models/Test'); // path 
var test = new Test({ 
    email: req.body.title, 
    password: req.body.testType 
}); 

test.save(function(err,data){ 

    cb(data); 

}); 
+0

{data:req.body}を試しましたが、同じエラーが発生します。 –

+0

あなたのやり方が間違っているからです。コンソールで印刷しようとすると、{req.body} - 渡すオブジェクトが間違ったタイプです。 – yojna

0

私はあなたがパラメータとして「テスト」のインスタンスを渡しているが、あなたの代わりにtestのインスタンスとしてTestを使用していると思います。

あなたはこの希望を試すことができます。ちょうどそれをダミーのレコードでテストしてからそれがうまくいけば、あなたのマングーススキーマに問題があることを意味するのですか?

router.route('/createtests').post(function (req, res, next) { 

     var Test = new Test(req.body); 
     postTest(Test, function (data) { 

      res.json({'message': 'The test created sucessfully'}); 
     }); 
    }); 

    var postTest = function(test, cb){ 

     test.save(function(err,data){ 
      if(!err) 
      cb(null,data); 
     }); 
    }; 
関連する問題