【问题标题】:Meteor: Trying to add custom field to users() but it didn't successMeteor:尝试向 users() 添加自定义字段,但没有成功
【发布时间】:2016-06-13 19:43:56
【问题描述】:

我一直在尝试将这些自定义字段添加到配置文件下的流星用户帐户系统中,但没有成功。我有一个模态表单,它会弹出来执行此操作,下面是我的事件代码。

Template.profile.events({
'submit #saveBasicProfile': function(event, template) {
    console.log("CLICK");
    event.preventDefault();
    var fullnameVar = event.target.fullname.value;
    var titleVar = event.target.title.value;
    var about_youVar = event.target.about_you.value;
    Meteor.users.update( {_id:Meteor.user()._id}, 
            { $set: 
                [{ "profile.fullname"  : fullnameVar }, 
                 { "profile.title"     : titleVar },
                 { "profile.about_you" : about_youVar } ]
            });

    //Router.go('/profile');
}   

});

【问题讨论】:

  • 您在用户集合的任何地方都有拒绝规则吗?配置文件是一种特殊情况,配置文件所有者应该可以直接从客户端编辑,无论您是否安装了不安全。

标签: meteor


【解决方案1】:

在方法中进行更新:

Template.profile.events({
    'submit #saveBasicProfile': function(event, template) {
       event.preventDefault();
       Meteor.call('update_profile',
           event.target.fullname.value,
           event.target.title.value,
           event.target.about_you.value,
           function(err) {
               if (err) alert(err);
               else Router.go('/profile');
           }
       );
    })
});

if (Meteor.isServer) {
    Meteor.methods({
        update_profile: function(fullname, title, about_you) {
            check(fullname, String);
            check(title, String);
            check(about_you, String);

            // ... further validation

            Meteor.users.update(Meteor.userId(), { $set: {
                'profile.fullname': fullname,
                'profile.title': title,
                'profile.about_you': about_you   
            }});
        })
    });
}

【讨论】:

  • 嘿@sba 我在这里尝试了你的方法,但我从浏览器Error: Internal server error [500] 收到此错误消息。我错过了什么吗?
  • 我在控制台中发现了这个错误Exception while simulating the effect of invoking 'update_profile' TypeError: Cannot read property 'update' of undefined(…) TypeError: Cannot read property 'update' of undefined
  • 好吧,我没有运行代码,所以很可能它有问题。检查服务器日志。浏览器日志中的错误似乎是因为 Meteor.user 在客户端上不可用。因此,这应该可以通过将其限制在服务器上来避免。我已经更新了我的答案,所以该方法只在服务器上定义,但它仍然未经测试,所以服务器上的 500 仍然存在。
【解决方案2】:

也许您尝试更新的个人资料与请求更新的个人资料不同。因此,您可以为用户模型设置允许规则,使所有用户都可以编辑配置文件字段。

类似的东西:

Users.allow({
// clients can modify the profile field of their own document, and
// nothing else.
update: function (userId, user, fields, modifier) {
    // make sure it is our record
    if (user._id == userId)
        return true;
    else
        if(fields[0] === 'profile')
            return true;

    return false;
}
);

但是,正如 Areca 所说,这是一个安全漏洞,如果它在生产中,您应该在服务器中创建一个方法并从客户端调用它。

【讨论】:

  • 除非问题已被编辑,否则它应该始终尝试更新当前用户的个人资料,因为它正在使用Meteor.user()._id更新个人资料
  • 不是真的..他可以添加如上所示的允许规则..docs.meteor.com/#/full/allow
  • 但它是用 {_id:Meteor.user()._id} 作为查询参数调用的,所以除非问题不同,否则它永远不会是不同的 ID。更不用说这将允许对用户对象中的任何字段进行任何操作,只要您指定的第一个字段是配置文件,您就可以对任何用户的任何字段执行任何您想要的其他操作...
【解决方案3】:

您很可能会收到“拒绝访问”,因为不允许客户端编辑用户集合。您可以添加更新的允许规则,但不建议这样做,因为可能存在安全漏洞。

您可以在模板中调用服务器方法并在服务器上处理您的更新操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2011-03-19
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多