0

I get this error with Next.js Here is my question schema (Question.js):

const mongoose = require('mongoose')

const questionModel = mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
    required: true
  },
  title: {
    type: String,
    required: true
  },
  body: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  likes: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
    required: true
  }],
  tags: [{
    type: String,
    required: true
  }]
})

const Question = mongoose.model('question', questionModel)

module.exports = Question

There's this similar question: here, but it didn't work to me.
Does anyone have a solution?

Denis Onu
  • 41
  • 1
  • 2
  • Did you check https://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose?rq=1 ? It may be because you require Question in 2 files, with different capitalization. – Minsky Jul 01 '21 at 10:34
  • Actually I fixed it by adding this line ```mongoose.models = {}``` – Denis Onu Jul 01 '21 at 12:41

0 Answers0