如何为所有模式注册猫鼬插件?

G. *_*ard 4 mongoose mongodb node.js mongoose-schema

我想念什么?Mongoose的文档说mongoose.plugin()为所有模式注册了一个插件。这是行不通的。我可以在每个架构上注册我的插件。

我的插件:

module.exports = function (schema, options) {

    schema.set('toObject',{
        transform: function (doc, ret, options) {
            return {
                test: 'It worked!'
            };
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

我的架构:

var testPlugin = require('test-plugin.js');

var personSchema = mongoose.Schema({

    _id : { type: String, default: $.uuid.init },

    ssn : { type: String, required: true, trim: true },

    first : { type: String, required: true, trim: true },
    middle : { type: String, required: true, trim: true },
    last : { type: String, required: true, trim: true }
});
personSchema.plugin(testPlugin);

var model = mongoose.model('Person', personSchema);

module.exports = model;
Run Code Online (Sandbox Code Playgroud)

不幸的是,上面的代码有效。但是,以下代码不会:

var personSchema = mongoose.Schema({

    _id : { type: String, default: $.uuid.init },

    ssn : { type: String, required: true, trim: true },

    first : { type: String, required: true, trim: true },
    middle : { type: String, required: true, trim: true },
    last : { type: String, required: true, trim: true }
});

var model = mongoose.model('Person', personSchema);

module.exports = model;
Run Code Online (Sandbox Code Playgroud)

我的测试应用程序:

var testPlugin = require('test-plugin.js');
mongoose.plugin(testPlugin);

mongoose.Promise = global.Promise;
mongoose.connect(config.db);
mongoose.connection.on('error', function (err) {
    if (err) { throw err; }
});
mongoose.connection.once('open', function (err) {
    if (err) { throw err; }

    seeds.doSeed(function(err){
        if (err) { return process.exit(1); }

        models.Person.find({}, function(err, people){
            if (err) { throw err; }

            var person = people[0];
            var oPerson = person.toObject();

            console.log(JSON.stringify(oPerson));
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

我已经试过移动mongoose.plugin(testPlugin)遍布app.js文件...后连接,等等......并没有奏效。

And*_*han 5

可能未注册插件,mongoose.plugin(myMongoosePlugin)因为在全局注册插件之前创建了猫鼬模型。

  • 如果您有expressjs路线:

在app.js (server.js)注册/创建expressjs路由(使用猫鼬模型与数据库进行通信)之前,请确保您正在注册猫鼬插件。

例:

在 app.js

const express = require(express);
const mongoose = require('mongoose');
const myMongoosePlugin = require('<Mongoose Plugin file path>');

mongoose.plugin(myMongoosePlugin);

let app = express();

//register expressjs routes
require('<Express routes file path>')(app, express.Router());

// or create expressjs routes
app.post('/person', (req, res, next) => {
    //where someMethod is using person mongoose model
    this.someController.someMethod(someArguments)
        .then((user) => {
            res.json(user);
        }).catch((error) => {
            next(error);
        });
});

// ... Some other code ...
mongoose.connect(<databaseConnectionString>);
app.listen(<Port>);
Run Code Online (Sandbox Code Playgroud)


Pet*_*tar 1

尝试在 app.js 文件中也要求您的模型。之后的某个地方mongoose.plugin(testPlugin)。