Nivas Dhina
Nivas Dhina

Reputation: 149

Model is not a function error in postman

Iam doing basic crud operation in mongodb when i try to insert new post to db i get a message in post man as Test is not a function.

My router function is as follow.

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);

    });

};

My schema is as follows.

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 getting a response as follow

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

Upvotes: 3

Views: 1951

Answers (3)

abdulbari
abdulbari

Reputation: 6242

I think you are passing an instance of "Test" as a parameter but you are using Test as instance instead of test.

you can try this hope that will work. Since just tested it with dummy record and it works and if it doesn't make sense mean your mongoose schema has issue

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

Upvotes: 0

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

In your function postTest , you have test with 't' and you are saving with 'T'(Test.save()) : uppercase/lowercase typo. this is what is causing your issue.

var postTest = function(test, cb){

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

        cb(data);

    });

};

Also, change common.conn.model to common.model

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

EDIT

You are using Test as variable name and model name both. change the var to test. It should solve your issue.

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'});

    });

});

Upvotes: 1

yojna
yojna

Reputation: 513

You need to write code in correct way.

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);

});

Upvotes: 0

Related Questions