我无法理解这一行:this.callQueue = [ [ 'pre', [ 'save', beforeSave ] ] ];
以下是它所在的行:
var util = require('util');
var Schema = require('mongoose').Schema;
var counter = require('./counter');
var utils = require('./utils');
/**
* Base Schema constructor.
*
* ####Example:
*
* var user = new BaseSchema({ name: String });
* var post = new BaseSchema({ content: String });
*
* // setting schema options
* new BaseSchema({ name: String }, { _id: false, autoIndex: false })
*
* ####Options:
*
* - [autoIndex](/docs/guide.html#autoIndex): bool - defaults to true
* - [bufferCommands](/docs/guide.html#bufferCommands): bool - defaults to true
* - [capped](/docs/guide.html#capped): bool - defaults to false
* - [collection](/docs/guide.html#collection): string - no default
* - [id](/docs/guide.html#id): bool - defaults to true
* - [_id](/docs/guide.html#_id): bool - defaults to true
* - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
* - [read](/docs/guide.html#read): string
* - [safe](/docs/guide.html#safe): bool - defaults to true.
* - [shardKey](/docs/guide.html#shardKey): bool - defaults to `null`
* - [strict](/docs/guide.html#strict): bool - defaults to true
* - [toJSON](/docs/guide.html#toJSON) - object - no default
* - [toObject](/docs/guide.html#toObject) - object - no default
* - [versionKey](/docs/guide.html#versionKey): bool - defaults to "__v"
*
*
* @param {Object} definition
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
* @event `init`: Emitted after the schema is compiled into a `Model`.
* @api public
*/
function BaseSchema() {
Schema.apply(this, arguments);
this.callQueue = [ [ 'pre', [ 'save', beforeSave ] ] ];
this.add({
_id: Number,
createTime: Number,
modifyTime: Number
});
}
/**
* set default value before save to db
*
*
* @param {Function} next
*/
function beforeSave(next) {
var model = this;
if(model.isNew) {
model.modifyTime = model.createTime = Date.now();
counter.getNextId(model.collection.name + '_id', function(err, id) {
model._id = id;
next();
});
}
else {
model.modifyTime = Date.now();
next();
}
}
util.inherits(BaseSchema, Schema);
module.exports = BaseSchema;
另外我认为用法就像这样
var Post = new BaseSchema({
title: String,
tags: Array,
content: String,
creator: String,
avatar: String,
comments: [Comment],
pv: Array,
reprint: { type: Object, default: {} },
lastUpdateTime: String
}, options);
Post.pre('save', function(next) {
if(this.isNew) {
this.lastUpdateTime = utils.formatDate(Date.now());
}
next();
})
此处还有项目github链接:[https://github.com/smadey/node-mongoose-blog]。代码在模型文件
中答案 0 :(得分:0)
this.callQueue = [ [ 'pre', [ 'save', beforeSave ] ] ]
在内部为从其继承的所有模式添加预保存中间件。例如,Post
架构现在将附加两个预保存挂钩:
beforeSave
BaseSchema
挂钩
Post
架构内部所有挂钩都存储在callQueue
数组中。如果您记录该变量,那么您将看到这两个钩子的两个回调:
> console.log(Post.callQueue)
[ [ 'pre', [ 'save', [Function: beforeSave] ] ],
[ 'pre', { '0': 'save', '1': [Function] } ] ]