【问题标题】:How do I get keystoneJS to run a function once a specific model has been updated?更新特定模型后,如何让 keystoneJS 运行函数?
【发布时间】:2016-07-17 19:23:02
【问题描述】:

一旦我在特定的 KeystoneJS 模型中有一个新的或更新的项目,我想运行一个函数。我怎么做?我要添加一个活动吗...已经有活动了吗?我是在模型中还是在其他地方添加它?

【问题讨论】:

    标签: javascript node.js express keystonejs


    【解决方案1】:

    您可以将 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 无关。

    【讨论】:

    • 感谢@Creynders 工作得很好!对于其他寻找快速 sn-p 的人,它是 Model.schema.pre('save', function(next){ console.log('SAVING USER:', this); next(); }); Model.schema.post('save', function(user){ console.log('USER WAS SAVED:', user); });
    【解决方案2】:

    KEystoneJS 使用猫鼬。

    当您添加新模型时,CRUD(change,Replace,Update,Delete) 在管理端自然发生(http:///keystone

    但是对于非管理员而言,您需要创建路由,并且在路由视图中您可以使用 mongoose API 来执行此操作。

    keystoneJS 中的任何更改,只需重新启动服务器(nam start)即可生效

    【讨论】:

    • 谢谢,我想知道如何为管理员端运行更新。这也可以用猫鼬中间件来完成吗?你知道我怎么能把它挂起来吗?我的问题是我不知道如何集成它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    相关资源
    最近更新 更多