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

时间:2016-08-17 15:06:54

标签: node.js mongodb mongoose mongoose-schema

我错过了什么? Mongoose文档说mongoose.plugin()为所有模式注册一个插件。这不起作用。我可以在EACH架构上注册我的插件。

我的插件:

module.exports = function (schema, options) {

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

我的架构:

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;

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

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;

我的测试应用:

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));
        });
    });
});

我已经尝试在mongoose.plugin(testPlugin)文件上移动app.js ...在连接等之后...并且没有任何效果。

3 个答案:

答案 0 :(得分:3)

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

  • 如果您有expressjs路线:

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

示例:

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>);

答案 1 :(得分:1)

尝试在app.js文件中同时使用您的模型。在data.frame之后的某个地方。

答案 2 :(得分:0)

这不是最佳解决方案,但可以解决,您必须在每个文件中定义Use private Maven repository,然后导出该文件

-SNAPSHOT

然后您应该只实现一个文件来设置模型,例如

Schema