IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    sequelize 上遇到的一些坑

    thomas0836发表于 2016-08-03 02:11:53
    love 0
    var User = db.define('user', {
        username: {type: Sequelize.STRING, allowNull: false, unique: 'compositeIndex'},
        password: {type: Sequelize.STRING, allowNull: false, },
        lastAt: {type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW,},
        roleType: {
            type:   Sequelize.INTEGER,
            allowNull: false,
            defaultValue: Enumeration.roleType[3].value,
    
        },
    },{
        indexes: [
            // Create a unique index on username,password
            {
                unique: true,
                fields: ['username','password']
            }
        ],
        getterMethods   : {
            roleTypeTitle : function() { return Enumeration.roleType[this.roleType].title },
            lastAtFormat : function() { return tools.formatDate(this.lastAt) },
            createdAtFormat: function() { return tools.formatDate(this.createdAt) },
            updatedAtFormat: function() { return tools.formatDate(this.updatedAt) },
        },
    })
    
    
    var User = db.define('user', {
        username: {type: Sequelize.STRING, allowNull: false, unique: 'compositeIndex'},
        password: {type: Sequelize.STRING, allowNull: false, },
        lastAt: {type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW,},
        roleType: {
            type:   Sequelize.INTEGER,
            allowNull: false,
            defaultValue: Enumeration.roleType[3].value,
    
        },
    },{
        indexes: [
            // Create a unique index on username,password
            {
                unique: true,
                fields: ['username','password']
            }
        ],
        getterMethods   : {
            roleTypeTitle : () => Enumeration.roleType[this.roleType].title,
            lastAtFormat :  () =>  tools.formatDate(this.lastAt) ,
            createdAtFormat:  () => tools.formatDate(this.createdAt) ,
            updatedAtFormat:  () => tools.formatDate(this.updatedAt) ,
        },
    })
    
    

    以上两段代码,折腾了一个晚上,第二段代码 使用es6的写法, 里面的this 对象 和 第一段代码中的this 对象不一样的。

    请问,这里的getterMethods 如果用 es6的 写法,怎样去获取原来的那个this?



沪ICP备19023445号-2号
友情链接