gpt4 book ai didi

node.js - 编写 Mongoose 插件- plugin() 方法?

转载 作者:可可西里 更新时间:2023-11-01 09:31:20 26 4
gpt4 key购买 nike

我有兴趣编写一个 mongoose 插件来使所有字段成为必需的。我知道还有其他方法可以做到这一点,但我喜欢编写自己的插件的想法。

来自文档 http://mongoosejs.com/docs/plugins :

// game-schema.js
var lastMod = require('./lastMod');
var Game = new Schema({ ... });
Game.plugin(lastMod, { index: true });

但是当我从我的模式创建模型并查看属性时,我没有看到 plugin() 方法:

var mongoose = require('mongoose');
var CpuSchema = require("../schemas/cpu");

var Cpu = mongoose.model('Cpu', CpuSchema);
console.log(Cpu);

module.exports = Cpu;

one@demo ~/cloudimageshare-monitoring/project $ node /home/one/cloudimageshare-monitoring/project/app/data/models/cpu.js  
{ [Function: model]
base:
{ connections: [ [Object] ],
plugins: [],
models: { Cpu: [Circular] },
modelSchemas: { Cpu: [Object] },
options: { pluralization: true } },
modelName: 'Cpu',
model: [Function: model],
db:
{ base:
{ connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
collections: { cpus: [Object] },
models: {},
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
discriminators: undefined,
schema:
{ paths:
{ timeStamp: [Object],
avaiable: [Object],
status: [Object],
metrics: [Object],
_id: [Object],
__v: [Object] },
subpaths: {},
virtuals: { id: [Object] },
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
statics: {},
tree:
{ timeStamp: [Object],
avaiable: [Function: Boolean],
status: [Function: String],
metrics: [Object],
_id: [Object],
id: [Object],
__v: [Function: Number] },
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
options:
{ id: true,
noVirtualId: false,
_id: true,
noId: false,
read: null,
shardKey: null,
autoIndex: true,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
_events: {} },
options: undefined,
collection:
{ collection: null,
opts: { bufferCommands: true, capped: false },
name: 'cpus',
conn:
{ base: [Object],
collections: [Object],
models: {},
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
_listening: false },
queue: [ [Object] ],
buffer: true } }

在模型上,我没有看到 plugin() 方法。

最佳答案

plugin 方法在 Schema 类上定义,您可以在 CpuSchema 对象上看到它。在您的 Cpu 模型上,您可以通过调用

来获取它
console.log(Cpu.schema.plugin)

来自mongoose source GitHub 上的代码:

/**
* Registers a plugin for this schema.
*
* @param {Function} plugin callback
* @param {Object} opts
* @see plugins
* @api public
*/

Schema.prototype.plugin = function (fn, opts) {
fn(this, opts);
return this;
};

当您将 plugin 函数传递给它时,它只会执行函数并将 schema 引用传递给它。

关于node.js - 编写 Mongoose 插件- plugin() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com