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

Plugins usage mongoose.model(name) not working #27

Closed
zinzinday opened this issue Jan 6, 2019 · 4 comments
Closed

Plugins usage mongoose.model(name) not working #27

zinzinday opened this issue Jan 6, 2019 · 4 comments

Comments

@zinzinday
Copy link

I'm submitting a...


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

@see Example

import * as existsValidator from 'mongoose-exists';
import * as uniqueValidator from 'mongoose-beautiful-unique-validation';
@Injectable()
export class MongoService implements MongooseOptionsFactory {
  createMongooseOptions(): MongooseModuleOptions {
    mongoose.plugin(existsValidator);
    mongoose.plugin(uniqueValidator);
    mongoose.set('debug', true);
    return {
      uri: 'mongodb://localhost:27017/testdb',
      useNewUrlParser: true,
      useCreateIndex: true,
    };
  }
}

App Module:

    MongooseModule.forRootAsync({
      useClass: MongoService,
    })

Schema

export const FavoritSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user',
    validate: {
      isAsync: true,
      validator: (v, cb) => {
        /*setTimeout(function() {
          cb(true);
        }, 500);*/ // working

        // mongoose.model('user').findById(v).exec().then(user => cb(!!user)).catch(() => cb(false));
        // not working
      },
      message: 'do not exists!',
    },
    required: true,
  },
}, {
  toJSON: {
    versionKey: false,
    virtuals: true,
    transform: (doc, ret) => {
      delete ret._id;
    },
  },
});

Schema usage plugin mongoose-exists

export const FavoritSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user',
    exists: true // not working
}, {
  toJSON: {
    versionKey: false,
    virtuals: true,
    transform: (doc, ret) => {
      delete ret._id;
    },
  },
});

Environment


[System Information]
OS Version     : Linux 4.15
NodeJS Version : v8.14.0
NPM Version    : 6.4.1
[Nest Information]
mongoose version : 5.2.2
common version   : 5.4.0
core version     : 5.4.0
"mongoose": "^5.4.2",
"mongoose-exists": "^0.6.4",

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

@zinzinday
Copy link
Author

that will working if we use method connect instead of createConnection at

mongoose.createConnection(uri, mongooseOptions as any),

@kamilmysliwiec
Copy link
Member

In this case, are you sure that this issue comes from the @nestjs/mongoose library? I'd say that this behavior should be described in the mongoose repository. Or plugins aren't applied to connections created through createConnection by default?

@NenadJovicic
Copy link

Actually, having same problem, and find out that it comes from nestjs somehow
#109

You can check my latest answer here.
So, is there any proper way to provide a proper connection to some plugin?
I am using this plugin
And it also requires mongoose.connection, which is undefined
But 2nd connection in mongoose.connections is valid one

@NenadJovicic
Copy link

@kamilmysliwiec looks like this is a 'problem' made by mongoose. because when you import mongoose in some file, it will automatically call constructor of mongoose, where it has createConnection() call without any params, and it will be pushed to list of connections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants