【问题标题】:Update element from user profile in Meteor从 Meteor 中的用户配置文件更新元素
【发布时间】:2015-11-07 22:00:42
【问题描述】:

我正在尝试使用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

我错过了什么?

【问题讨论】:

    标签: javascript mongodb meteor meteor-accounts


    【解决方案1】:

    如果您使用的是dot notation,则需要将整个虚线字段名称括在引号中。

    在你的情况下:

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

    阅读更多关于updating an embedded field的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 2016-04-21
      • 2022-01-03
      • 1970-01-01
      • 2014-06-05
      • 2022-01-07
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多