在 Meteor 中更新用户帐户客户端时访问被拒绝 [403]

Access denied [403] when updating user accounts client-side in Meteor

我正在阅读 Meteor here and the useraccounts package here 的文档,但找不到答案。我已经成功添加了 useraccounts 包并创建了一些用户,但现在我想将一些数据添加到给定用户的集合中的记录中。

例如,在帐户创建和登录之后。我希望用户能够 add/edit 他们记录中的某些字段(简短的传记等),但是每当执行 Meteor.users.update(..).

时我总是收到 403 错误

可以找到我的登录配置文件here

导致错误的代码:

Template.editProfile.events({
    'submit form': function(e) {
        e.preventDefault();

        var profileInfo = {
            displayName: $(e.target).find('[name=displayName]').val(),
            tagLine: $(e.target).find('[name=tagLine]').val(),
            aboutMe: $(e.target).find('[name=aboutMe]').val()
        };

        Meteor.users.update(
            { _id: Meteor.userId()},
            { $set: profileInfo},
            function (err) {
                if(err) {
                    console.log('there was an error submitting editProfile data');
                    console.log(err);
                } else {
                    Router.go('profile');
                }
            }
        );
    }
});

控制台日志显示 Meteor.userId() 正确返回,所以我不确定问题出在哪里。我假设这是 allow/deny 的问题,但我什至不知道从哪里开始进行故障排除。

确切的错误是:

error: 403

errorType: "Meteor.Error"

message: "Access denied [403]"

reason: "Access denied"

通过删除 insecure 包,默认情况下将拒绝客户端写访问。 如果要允许客户端直接写入集合,则需要定义规则。

例如:

Meteor.users.allow({
    update: ownsDocument
});

ownsDocument = function (userId, doc) {
    return doc && doc.userId === userId;
};

ownsDocument() 函数检查指定的 userId 是否拥有该文档。除了 update 回调之外,您还可以为 insertremove.

设置规则

阅读有关 Meteor's collection.allow(options), access a demo app or clone the repository 的更多信息。