【发布时间】:2016-07-17 19:23:02
【问题描述】:
一旦我在特定的 KeystoneJS 模型中有一个新的或更新的项目,我想运行一个函数。我怎么做?我要添加一个活动吗...已经有活动了吗?我是在模型中还是在其他地方添加它?
【问题讨论】:
标签: javascript node.js express keystonejs
一旦我在特定的 KeystoneJS 模型中有一个新的或更新的项目,我想运行一个函数。我怎么做?我要添加一个活动吗...已经有活动了吗?我是在模型中还是在其他地方添加它?
【问题讨论】:
标签: javascript node.js express keystonejs
您可以将 mongoose 中间件与任何非 keystone 项目一起使用。可以使用 .schema 访问 keystone 列表架构,例如
var keystone = require('keystone');
var Types = keystone.Field.Types;
var User = new keystone.List('User');
User.add({
name: { type: Types.Name, required: true, index: true },
email: { type: Types.Email, initial: true, required: true, index: true },
});
//do stuff BEFORE the user document is fully saved to the DB
User.schema.pre('save', function(next){
console.log('SAVING USER:', this);
next();
});
//do stuff AFTER the user document has been saved to the DB
User.schema.post('save', function(user){
console.log('USER WAS SAVED:', user);
});
User.defaultColumns = 'name, email';
User.register();
看看mongoose middleware,因为有一些限制,例如在进行大规模更新时,中间件将不运行,这是猫鼬的设计,与 keystone 无关。
【讨论】:
Model.schema.pre('save', function(next){ console.log('SAVING USER:', this); next(); }); Model.schema.post('save', function(user){ console.log('USER WAS SAVED:', user); });
KEystoneJS 使用猫鼬。
当您添加新模型时,CRUD(change,Replace,Update,Delete) 在管理端自然发生(http:///keystone
但是对于非管理员而言,您需要创建路由,并且在路由视图中您可以使用 mongoose API 来执行此操作。
keystoneJS 中的任何更改,只需重新启动服务器(nam start)即可生效
【讨论】: