Mongoose: what's up with “_doc”?

前端 未结 4 1191

It seems Mongoose is doing something really funky internally.

1 var Foo = new mongoose.model(\'Foo\', new mongoose.Schema({a: String, b: Number}));
2 var foo         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 13:32

    There is some weirdness with Mongoose models and you have to check that Mongoose doesn't already have a model created in it's models array.

    Here is my solution:

    import mongoose from 'mongoose';
    createModel = (modelName="foo", schemaDef, schemaOptions = {})=> {
      const { Schema } = mongoose;
      const schema = Schema(schemaDef, schemaOptions);
      const Model = mongoose.models[modelName] || mongoose.model(modelName, schema);
      return Model;
    }
    

    I use my own mongoose model class and base class for my models. I made this and it should work for you.

提交回复
热议问题