Javascript 初始化模型实例而不将其持久化到数据库中
我正在尝试创建一个水线模型实例,以便以后可以为其属性赋值,并对其调用Javascript 初始化模型实例而不将其持久化到数据库中,javascript,sails.js,waterline,Javascript,Sails.js,Waterline,我正在尝试创建一个水线模型实例,以便以后可以为其属性赋值,并对其调用.save()。 在lib/waterline/model/lib/model.js中,有一条文档注释说明: *var Person=模型原型; *var person=newperson({name:'foobar'}); *person.name#=>“Foo-Bar” 我有一个模型用户,但我不能做任何这些陈述 User.prototype给了我undefined和var User=new User({firstName
.save()
。
在lib/waterline/model/lib/model.js
中,有一条文档注释说明:
*var Person=模型原型;
*var person=newperson({name:'foobar'});
*person.name#=>“Foo-Bar”
我有一个模型用户,但我不能做任何这些陈述
User.prototype
给了我undefined
和var User=new User({firstName:'Foo'})
给了我一个错误:TypeError:用户不是构造函数
。
我在WaterlineGithub上读过一些文章,但似乎没有什么适合我的
真的没有办法吗
编辑:
这是我当前的用户模型定义
/**
* 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();
});
}
};
根据sails文档,所有模型都应该在sails应用程序中定义为globals。你可以附加你的User.js文件吗?我已经添加了模型定义。你是否在全局配置文件中设置了models=true:当然,这些模型可以作为全局变量访问,我可以使用它们从数据库中查找、创建、更新和删除对象。我唯一不能也应该做的事情(出于主要帖子中提到的原因)是
newmodel()
。我知道官方文档中没有提到这一点,但是有一些方法来初始化waterline模型实例是有意义的