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

257 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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

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