我对这些看似微不足道的东西遇到了麻烦,哈哈! 我有这个用户文档:
userData = {
account: {
type: 'free'
},
profile: {
name: 'Artem',
},
username: 'aaa@gmail.com',
password: '123'
};
我发送客户端的内容:Accounts.createUser(userData);
然后服务器端我想检查帐户类型是否等于'免费'。如果它没有 - 我想中止新用户创建(并希望抛出错误客户端)
我在文档中找到了两个函数,可能有助于我这样做:
profile, username, password, email
以外的属性。因此,我无法验证account.type
,因为它在验证的用户对象上不存在。undefined
,它会在服务器上抛出错误:
Exception while invoking method 'createUser' Error: insert requires an argument
答案 0 :(得分:0)
您可以使用Accounts.validateNewUser
对数据结构进行少许更改:
userData = {
profile: {
name: 'Artem',
account : {
type : 'free'
}
},
username: 'aaa@gmail.com',
password: '123'
};
然后您应该能够访问所需的数据。
据我记得,有关移除profile
字段的流星论坛有一些讨论,这就是为什么我以不同方式解决这类问题的原因。对我来说Meteor.users
是为了和平而不应该改变的集合 - 它可以被未来版本的流星改变。我的方法需要在开始时编写更多代码,但稍后它会得到回报,因为您可以存储有关用户的数据,Meteor.users
集合具有文档数量最少的文档。
我会使用jagi:astronomy@0.12.1来创建架构和自定义方法。一般情况下,我会使用架构创建新的集合UserAccounts
:
UserAccount = new Astro.Class( {
name: 'UserAccount',
collection: 'UserAccounts',
fields: {
'userId' : {type: 'string'},
'type' : {type: 'string', default:'free'}
},
} )
并将架构添加到Meteor.users
:
User = new Astro.Class( {
name: 'User',
collection: Meteor.users,
fields: {
'services' : {type: 'object'},
'emails' : {type: 'array'}
},
methods:{
account : function(){
return UserAccounts.findOne({userId:this._id})
}
}
} )
用法如下:
var user = Meteor.users.findOne();
user.account().type
总结:
UserAccount
(使用字段userId
)