从 Meteor 中的用户配置文件更新元素

Update element from user profile in Meteor

我正在尝试使用 onCreateUser:

更新在 user.profile 中创建的 firstName 字段
Accounts.onCreateUser(function(options, user) {
    user.profile = options.profile || {};
    user.profile.firstName = options.firstName;
    return user;
});

我也使用允许:

Meteor.users.allow({
    update: function(userId, doc, fields, modifier) {
        if (fields === "profile.firstName") {
            return true;
        }
    }
});

当我使用:

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

可以用,但不是我需要的。

我需要的是更新firstName:

Meteor.users.update({
    _id: Meteor.userId()
}, {
    $set: {
        profile.firstName: "George"
    }
});

但是我得到这个错误:

SyntaxError: missing : after property id

我错过了什么?

如果您使用的是 dot notation,则需要将整个带点的字段名称用引号引起来。

你的情况:

Meteor.users.update({
    _id: Meteor.userId()
}, {
    $set: {
        "profile.firstName": "George"
    }
});

详细了解 updating an embedded field