node.jsmongodbexpressmongoose-schemamongoose-web-server

Mongoose if (!this.modelSchemas[name]) error


Whenever I try connecting using the code below,

const mongoose = require('mongoose');
 
// var Model = mongoose.model.bind(mongoose); didnt work

var imageSchema = new mongoose.Schema({
 
  name: String,
  dob: String,
  breed: String,
  details: String,
    img:
    {
        data: Buffer,
        contentType: String
    }
 
});

module.exports = new mongoose.model('Image', imageSchema);

I get this error logged in the console

if (!this.modelSchemas[name]) {
                        ^

TypeError: Cannot read property 'Image' of undefined

I had tried the model bind mongoose as suggested on another page, but that doesn't work as well


Solution

  • model is a method that returns the model. You shouldn't call it with new:

    module.exports = mongoose.model('Image', imageSchema);