Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

544
Visualizações
Mongoose Populate - Missing Schema Error, but the schema is properly registered for the model

Last day of 2021! After spending just over 5 hours scouring the internet I've resorted to posting for help.

Desired Behaviour:

  • the list of documents returned by the route will contain populated documents instead of only the ref ids

Attempted Resolutions:

  • added exec function after populate
  • Stackoverflow lead me down a rabbit hole of believing the issue was related to mongoose connections, and so I revamped the backend so that my model files returned schemas instead of models, and then used mongoose.createConnection instead of .connect, and made a db.js module to hold a single instance of each model (connection.mode()) that route files could then access and share. I did not reach a point where I could test .populate so I git reset back to the below code

Error

MissingSchemaError: Schema hasn't been registered for model "progressions".
Use mongoose.model(name, schema)

Note that this error is thrown when trying to populate "lesson" as well

Progress Model

const Progress = mongoose.model('Progress', new Schema({
  lesson: {
    type: Schema.Types.ObjectId,
    ref: 'lesson'
  },
  progressions: [{
    type: Schema.Types.ObjectId,
    ref: 'progression'
  }],
  progress:{
    type: Number
  },
  user:{
    type: Schema.Types.ObjectId,
    ref: 'user'
  }
},{
  timestamps: true,
}));

module.exports = Progress;

Progress Routes File

router.get('/all', async (req, res) => {
  let user = await User.find({email:req.query.user})
  let data = await Progress.find({user:user}).populate('progressions')
  return res.status(201).json({
     data,
    message: 'data populated',
    success: true
  });
});

Progression Model

const Progression = mongoose.model('Progression', new Schema({

  type:{
    type:String
  },
  toBeReviewed:{
    type: Date,
    default: Date.now
  },
},{
  timestamps: true,
}));

module.exports = Progression;
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try to change the progressions property ref attribute in the Progress schema to match the Progression model name (capitalize it):

progressions: [{
    type: Schema.Types.ObjectId,
    ref: 'Progression'
}]
over 4 years ago · Santiago Trujillo Relatório

0

All code in your mongoose files should have run before it is used in other files.

This error could be happening because your mongoose models aren't executed before they are used in some other file.

over 4 years ago · Santiago Trujillo Relatório

0

did you try using "Progressions" while passing it in populate ?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda