question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
igorivaniukcommented, Aug 20, 2019

@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));
});
0reactions
jmcdo29commented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't instance a model from mongoose after update from v4 to ...
My assumption is that somehow, mongoose.connection is not set globally, so, model does not know through which connection to execute query. Input ...
Read more >
Mongoose v6.8.1: Model
A Model is a class that's your primary tool for interacting with MongoDB. An instance of a Model is called a Document. In...
Read more >
Upgrade to the Latest Revision of MongoDB
When upgrading a binary, use the procedure Upgrade a MongoDB Instance. Follow this upgrade procedure: For deployments that use authentication, first upgrade all ......
Read more >
How do I update/upsert a document in Mongoose?
Mongoose now supports this natively with findOneAndUpdate (calls MongoDB findAndModify). The upsert = true option creates the object if it doesn't exist.
Read more >
Mongoose - npm
Mongoose MongoDB ODM. Latest version: 6.8.1, last published: 7 days ago. Start using mongoose in your project by running `npm i mongoose`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found