2013-05-17 59 views
1

我在将自定义用户字段添加到流星用户对象(Meteor.user)时遇到问题。我想要一个用户有一个“状态”字段,我宁愿不将它嵌套在“profile”(即profile.status)下,我知道默认情况下是r/w。 (我已经删除autopublish。)将自定义字段添加到流星用户时遇到特权问题

我已经能够场发布到客户端就好了通过

Meteor.publish("directory", function() { 
    return Meteor.users.find({}, {fields: {username: 1, status: 1}}); 
}); 

...但我不能设置权限允许对日志 - 用户更新自己的status

如果我做

Meteor.users.allow({ 
    update: function (userId) {  
    return true; 
}}); 

Models.js,用户可以编辑用户的所有领域。这并不酷。

我试着做变体,如

Meteor.users.allow({ 
    update: function (userId) {  
    return userId === Meteor.userId(); 
}}); 

Meteor.users.allow({ 
    update: function (userId) {  
    return userId === this.userId(); 
}}); 

,他们只是在控制台中我拒绝访问错误。

这个有点documentation addresses,但没有详细说明。我在犯什么愚蠢的错误?

(这类似于this SO问题,但这个问题只解决如何发布领域,而不是如何对其进行更新。)

回答

3

尝试:

Meteor.users.allow({ 
    update: function (userId, user) {  
    return userId === user._id; 
    } 
}); 

从收集的文档。允许:

更新(用户ID,DOC,FIELDNAMES,改性剂)

钍用户userId想要更新文档文档。 (doc是数据库中文档的当前版本,没有建议的更新。)返回true以允许更改。

5

这就是我如何运作的。

在服务器我发布用户数据

Meteor.publish("userData", function() { 
    return Meteor.users.find(
    {_id: this.userId}, 
    {fields: {'foo': 1, 'bar': 1}} 
); 
}); 

,并设置允许在客户端代码如下

Meteor.users.allow({ 
    update: function (userId, user, fields, modifier) { 
    // can only change your own documents 
    if(user._id === userId) 
    { 
     Meteor.users.update({_id: userId}, modifier); 
     return true; 
    } 
    else return false; 
    } 
}); 

,冥冥中我更新的用户记录,只要有一个用户

if(Meteor.userId()) 
{ 
Meteor.users.update({_id: Meteor.userId()},{$set:{foo: 'something', bar: 'other'}}); 
} 
+0

它不适合我...... userData代表什么? –

相关问题