简体   繁体   中英

Getting error while fetching all documents from a mongodb collection

I am getting this error 

> [ERROR] TypeError: undefined is not a function
while fetching all documents from a collection. 

My Code is below: 

Calender JS

Model: 

var taskCalenderSchema = new mongoose.Schema({
    user_id:{type:String, required: false, unique:false},
    task_data:{},
    from: {type: Date, required: false, unique:false},
    to: {type: Date, required: false, unique:false}
});
module.exports = mongoose.model('task_calender', taskCalenderSchema);
exports.schema = taskCalenderSchema;


Task JS

exports.getAllTasks = function(request, response){


    Calender.find({}, function(error, listOfTasks){
        if(error){
            response.json({status: false, msg : 'Some thing wrong while getting tasks list'});
        }else{
            response.json({status: true, data: listOfTasks});
        }
    });
};

I have written my code above. In model I have created the schemas. Saved documents but meanwhile retrieving I am getting this error I am getting this error :

undefined is not a function while fetching all documents from a collection.

Actually, I was not pointing reference accurately. Since in Calendar JS I have exports using

module.exports = mongoose.model('task_calender', taskCalenderSchema);

and In task Js I referenced it, In this way

var Calender = require('../../../ZemModels/Calender').model;

which was wrong. The correct syntax was

var Calender = require('../../../ZemModels/Calender');

In Short, When we exports a schema using module.exports then we no need to use model in referenced file. Model is used if we have exports a schema using model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM