Meteor.js& Mongo:您可以从客户端向user.profile中插入新文档或参数吗?

时间:2013-04-16 05:29:37

标签: javascript mongodb meteor

就像问题一样,可以这样做吗?

我想将用户链接的数组插入当前用户的个人资料中。我在客户端尝试了类似的操作,但它不起作用:

Meteor.users.update( {_id: Meteor.userId()}, {
        $set: {
            profile: {
                userlinks: {
                    owned: "_entry_id"
                }
            }
        }
    });

我也尝试用insert替换update但是没有用。

我怀疑它可能是什么:

  • 我没有正确设置mongodb权限(最不可能)
  • 我没有正确发布用户集合(我目前根本没有发布它,所以我认为流星会自动执行...但可能还不够吗?)(有点可能)
  • Insert是正确的命令但是这仅限于服务器,所以我必须在服务器上将插入函数放在Meteor方法中,然后从客户端调用该方法? (更有可能)

或许我只是不知道我在说什么(最有可能)

2 个答案:

答案 0 :(得分:3)

检查Meteor Update Docs您的语法错误。 尝试:

var id = Meteor.userId();
 Meteor.users.update( id, {
        $set: {
            profile: {
                userlinks: {
                    owned: "_entry_id"
                }
            }
        }
    });

答案 1 :(得分:1)

您可以这样做,如果您使用的是自定义帐户,则您的用户需要有一个配置文件字段用户界面确保在对用户进行身份验证时将配置文件设置为某个内容即使它只是一个空白对象来启动用:

var options = {
    username: 'some_user',
    password: 'password',
    email: 'email@domain.com',
    profile: {}
}

Accounts.createUser(options, function(err){
    if(err){
        //do error handling
    }
    else
        //success
});

如果您删除了不安全的软件包,则需要确保设置Meteor.users.allow

类似的东西:

Meteor.users.allow({
    update: function(userId, doc, fieldNames, modifier){
        if(userId === doc._id && fieldNames.count === 1 && fieldNames[0] === 'profile')
            return true;
    }
})

这样用户只能更新自己,并且只能更新个人资料字段。