简体   繁体   English

通过Meteor中的用户个人资料更新元素

[英]Update element from user profile in Meteor

I am trying to update firstName field created in user.profile using onCreateUser : 我正在尝试使用onCreateUser更新在user.profile创建的firstName字段:

Accounts.onCreateUser(function(options, user) {
    user.profile = options.profile || {};
    user.profile.firstName = options.firstName;
    return user;
});

Also I use allow: 我也使用allow:

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

When I use: 当我使用时:

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

It works but it is not what I need. 它有效,但这不是我需要的。

What I need is to update firstName : 我需要更新firstName

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

But I get this error: 但是我得到这个错误:

SyntaxError: missing : after property id SyntaxError:缺少:属性ID之后

What am I missing? 我想念什么?

If you are using the dot notation you need to enclose the whole dotted field name in quotes. 如果使用的是点符号 ,则需要将整个虚线字段名称括在引号中。

In your case: 在您的情况下:

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

Read more about updating an embedded field . 阅读有关更新嵌入式字段的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM