初始化模型实例而不将其保留在数据库中

时间:2016-06-21 11:18:51

标签: javascript sails.js waterline

我尝试创建水线模型实例,以便稍后为其属性分配值并在其上调用.save()。 在lib/waterline/model/lib/model.js中有一条文档评论说明:

* var Person = Model.prototype; * var person = new Person({ name: 'Foo Bar' }); * person.name # => 'Foo Bar'

我有一个模型用户,但我无法做任何这些陈述。

User.prototype给了我undefinedvar user = new User({firstName: 'Foo'})给了我一个错误:TypeError: User is not a constructor。 我已经阅读了关于水线github的一些问题,但似乎没有什么对我有用。

真的没办法吗?

编辑: 这是我当前的用户模型定义

/**
 * User.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {

    attributes: {
        username: {
            type: 'string',
            unique: true,
            alphanumericdashed: true
        },
        password: {
            type: 'string'
        },
        email: {
            type: 'string',
            //email: true,
            required: true,
            unique: true
        },
        firstName: {
            type: 'string',
            required: true
        },
        lastName: {
            type: 'string',
            required: true
        },

        company: {
            model: 'company'
        },
        //branches this user can access
        branches: {
            collection: 'branch',
            via: 'users'
        },
        roles: {
            collection: 'userrole',
            via: 'users'
        },
        //array of permission modifying permissions of any of roles assigned to this user
        userPermissions: {
            collection: 'permission',
            via: 'user',
            through: 'userpermission'
        },
        //daily reports this user has been marked in as employee
        dailyReports: {
            collection: 'dailyreport',
            via: 'dailyReportEmployees'
        },
        //events this user has been assigned to
        assignedEvents: {
            collection: 'event',
            via: 'assignees'
        },

        toJSON: function () {
            var obj = this.toObject();

            delete obj.password;

            return obj;
        }
    },

    beforeUpdate: function (values, next) {
        CipherService.hashPassword(values, function () {
            next();
        });
    },
    beforeCreate: function (values, next) {
        CipherService.hashPassword(values, function () {
            next();
        });
    }
};

0 个答案:

没有答案