Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't instance a model from mongoose after update from v4 to v6 #109

Closed
NenadJovicic opened this issue Jul 16, 2019 · 10 comments
Closed

Can't instance a model from mongoose after update from v4 to v6 #109

NenadJovicic opened this issue Jul 16, 2019 · 10 comments

Comments

@NenadJovicic
Copy link

NenadJovicic commented Jul 16, 2019

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Duplicated issue https://github.com/nestjs/nest/issues/2585
but I was not sure where should I actually report this bug, so, I am also opening it here

Current behavior

I was using v4 of nestjs and i was instancing new mongoose model with

import { model } from 'mongoose';

const someModel: Model = model(modelName, someSchema)

After upgrading nest to v6, this is not working anymore. I switched to officially suggested way of using it with @InjectModel(), but there is still a case where I have to instance model like this.
I want to write a prehook in which I want to do some query on some model.
But when I do it like this, it can't execute a query, and it does not throw an error. But it stops execution of code.

My assumption is that somehow, mongoose.connection is not set globally, so, model does not know through which connection to execute query.

Expected behavior

Expecting that using mongoose.model() still can execute query, or if it is possible somehow to extract connection from app.module

Minimal reproduction of the problem with instructions

import { model } from 'mongoose';

Schema.pre('save', function (next: Function): void {
   const someModel: Model = model(modelName, schema);
   someModel.countDocuments(query).then((result: number) => {
       return next(true);
   }).catch((error) => next(error));
});

Some example code, which should be executed, not providing full example, I think it is not necessary.
I can't use this https://docs.nestjs.com/recipes/mongodb, because I don't need this feature in some provider or module, so no option to inject it

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.4.0

 
For Tooling issues:
- Node version: 8.13 
- Platform: Linux, docker 

Others:

- Mongoose: 5.6.3
@kamilmysliwiec
Copy link
Member

Could you please provide a minimal repository which reproduces your issue? :)

@NenadJovicic
Copy link
Author

NenadJovicic commented Jul 18, 2019

https://github.com/NenadJovicic/test-nest-project
Here it is, minimal repo that will reproduce an issue
@kamilmysliwiec you just have to create some local db on your machine with users collection and maybe some data

@BorntraegerMarc
Copy link
Contributor

@kamilmysliwiec did you have time to look into this?

@NenadJovicic
Copy link
Author

still no update on this? @kamilmysliwiec

@igorivaniuk
Copy link

@NenadJovicic for now i doing something like this

export const getModel = name => {
  return mongoose.connections[1].model(name)
}

Schema.pre('save', function (next: Function): void {
   const someModel = getModel(modelName);
   someModel.countDocuments(query).then((result: number) => {
       return next(true);
   }).catch((error) => next(error));
});

@NenadJovicic
Copy link
Author

NenadJovicic commented Oct 4, 2019

@igorivaniuk thanks for help

@kamilmysliwiec tested out also with mongoose 5.0.1, because I used that version with nest v4, and there is a same problem.
With nest v4 and mongoose 5.0.1 mongoose.connections array does not exist at all
So, looks like problem that we have is something about your engine and how it sets connections.

because default connection for mongoose that is set up does not contain all collections that my db has, only 1 collection, and 2nd connection from list of mongoose.connections actually contains all models and collections that are in my DB

@NenadJovicic
Copy link
Author

@kamilmysliwiec I found a reason why is this happening, that we have 2 different connections with mongoose, and that 2nd one is only valid one, or connection to my DB.

It happened in this commit where you changed from mongoose.connect to mongoose.createConnection inside mongoose.core.module.ts
connect method was overriding previous connection, and createConnection is adding new connection to mongoose.connections array
What I don't understand is why 1st (not valid) connection is created. What I found out is, it calls createConnection immediately when I import MongooseModule in app.module.ts of my app
where uri for connection and options are undefined.

So, is there any way to fix this? To not add that first default connection on import or to return it to connect method

@kamilmysliwiec
Copy link
Member

You can inject the Connection into your class (see here https://docs.nestjs.com/techniques/mongodb#connection) and dynamically call model.

@kamalkech
Copy link

const someModel = mongoose.model(EventModel.name, EventSchema);
TypeError: Cannot read property 'model' of undefined

@jmcdo29
Copy link
Member

jmcdo29 commented Dec 11, 2021

const someModel = mongoose.model(EventModel.name, EventSchema); TypeError: Cannot read property 'model' of undefined

If I had to bet, it has to deal with how you import mongoose. Try a named import like import * as mongoose from 'mongoose'
For further support please use our Discord channel (Support). We are using GitHub to track Bug Reports, Feature Requests, and Regressions.

@nestjs nestjs locked and limited conversation to collaborators Dec 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants