Comments (10)
@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));
});
from mongoose.
Could you please provide a minimal repository which reproduces your issue? :)
from mongoose.
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
from mongoose.
@kamilmysliwiec did you have time to look into this?
from mongoose.
still no update on this? @kamilmysliwiec
from mongoose.
@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
from mongoose.
@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
from mongoose.
You can inject the Connection
into your class (see here https://docs.nestjs.com/techniques/mongodb#connection) and dynamically call model
.
from mongoose.
const someModel = mongoose.model(EventModel.name, EventSchema);
TypeError: Cannot read property 'model' of undefined
from mongoose.
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.
from mongoose.
Related Issues (20)
- Typescript 5 @InjectModel HOT 2
- Subdocuments Array has not id() and remove()-method HOT 3
- Don't extend `MongooseModuleOptions` from `Record<string, any>` HOT 4
- Using getters and setters with in super class HOT 1
- Issue with RXJS dependency (0 , rxjs_1.lastValueFrom) is not a function HOT 1
- How do I integrate @casl/mongoose and how do I operate to modify the Model? HOT 1
- How do I integrate @casl/mongoose, it doesn't seem to compile through. HOT 1
- NestJS: Mongo one to many relationship doesn't work HOT 1
- cache connection HOT 1
- Support compound index in @Schema decorator HOT 1
- Unexpected ref function behaviour HOT 1
- OverwriteModelError: Cannot overwrite `X` model once compiled. HOT 2
- @Prop() schemaType on Map not working HOT 3
- findOneAndUpdate fails with multi arrayFilters in nested arrays HOT 1
- Unable to connect for non-SSL connections when specifying dbName in URI HOT 1
- Update mongoose to v8 HOT 2
- InjectModel decorator for multiple databases is broken since version 9.1.0 HOT 1
- When set connectionName to useClass, Populate Error: Schema hasn't been registered for model HOT 1
- Injecting services into MongooseModule.forFeatureAsync not triggering the middleware hooks HOT 1
- @Prop() schemaType on Map not working.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.