0

I'm using mongoose to store information. My problem is that I need to find a previously registered model by it's name.

I register my model using the following source:

global.dbConn1.model('UserModel', new mongoose.Schema({
name: String
}, {versionKey: false}))

Later, I'm trying to access my model by it's name and query it:

mongoose.model('UserModel').findById('<ID>', console.log /* CALLBACK */)

The problem is that findById() never returns. I'm getting no output, hence the callback (console.log) is never called. But I'm getting no error and the userModel is also correct.

Any idea why the CALLBACK is never called?

Nrgyzer
  • 783
  • 1
  • 14
  • 38
  • Are you sure that you are connected to the database? – Orelsanpls May 15 '18 at 14:34
  • Yes, I'm sure because some other models are using the same connection and these models work. The only difference is that I'm using the mongoose-model directly and not via mongoose.model(). – Nrgyzer May 15 '18 at 14:38
  • I think that using `.model(...)` method require a schema to work – Orelsanpls May 15 '18 at 14:53
  • @GrégoryNEUT I think it should work without the schema because .model(...) is used by various plugins and also marked as the correct answer on https://stackoverflow.com/questions/8730255/how-to-get-schema-of-mongoose-database-which-defined-in-another-model – Nrgyzer May 15 '18 at 15:06
  • I see that you use of `dbConn1`, do they both use the same mongoose connection ? If you declared `UserModel` as a part of `dbConn1`, the main `mongoose` instance won't know it. It's my guess – Orelsanpls May 15 '18 at 15:19
  • @GrégoryNEUT Yes, all these models are using this connection. – Nrgyzer May 15 '18 at 15:21

0 Answers0