Mongoose Model.find is not a function?
Your module export is incorrect
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var listSchema = new Schema({
name: { type: String, default: datestring + " List" }
});
**mongoose.exports = mongoose.model('List', listSchema);** <!-- this is wrong -->
it should be
**module.exports = mongoose.model('List', listSchema)**
You've defined incorrect module.exports.
mongoose.exports = mongoose.model('List', listSchema);
This should be
module.exports = mongoose.model("List", listSchema);
i faced this issue . to solve this , you need to understand one logic .
you need to call .find
as promise to model which is imported from models file.
example:
const member = require('..// path to model')
//model initiation
const Member = new member();
exports.searchMembers = function (req,res) {
Member.find({},(err,docs)=>{
res.status(200).json(docs)
})
}
this code dont work because i called find()
to initiated schema
code that works :
exports.searchMembers = function (req,res) {
member.find({},(err,docs)=>{
res.status(200).json(docs)
})
}
here i called .find()
directly to imported model