Giter Site home page Giter Site logo

Comments (10)

igorivaniuk avatar igorivaniuk commented on May 2, 2024 1

@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.

kamilmysliwiec avatar kamilmysliwiec commented on May 2, 2024

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

from mongoose.

NenadJovicic avatar NenadJovicic commented on May 2, 2024

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.

BorntraegerMarc avatar BorntraegerMarc commented on May 2, 2024

@kamilmysliwiec did you have time to look into this?

from mongoose.

NenadJovicic avatar NenadJovicic commented on May 2, 2024

still no update on this? @kamilmysliwiec

from mongoose.

NenadJovicic avatar NenadJovicic commented on May 2, 2024

@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.

NenadJovicic avatar NenadJovicic commented on May 2, 2024

@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.

kamilmysliwiec avatar kamilmysliwiec commented on May 2, 2024

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

from mongoose.

kamalkech avatar kamalkech commented on May 2, 2024

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

from mongoose.

jmcdo29 avatar jmcdo29 commented on May 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.