Skip to content

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

Closed
@zinzinday

Description

@zinzinday

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:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions