Meteor:更新多个配置文件值

时间:2014-09-04 18:00:44

标签: javascript meteor

这是非常简单的实现,但我的语法可能存在一些问题:

Template.userProfilePage.events({
    'submit #profile-page': function(event, template) {
        event.preventDefault();
        var name = template.find('#fullName').value,
        address = template.find('#address').value,
        company = template.find('#company').value,
        otherField = template.find('#other').value;

        alert(name);

        Meteor.users.update(
            { _id:Meteor.user()._id },
            { 
                $set: {
                    "profile.name":name,
                    "profile.address":address,
                    "profile.company":company,
                    "profile.other":other
                }
            },
            { upsert: true },
            { multi: true }
        );

        return false;
    }
});

模板包含普通的html页面。它总是抛出错误:

  

RangeError: Maximum call stack size exceeded.

1 个答案:

答案 0 :(得分:1)

如果您只更新一个用户,则无需multi: true。也应该永远不需要坚持;如果您正在使用已登录的用户,则users集合中应始终有一个文档。尝试这样的事情:

Meteor.users.update(
  Meteor.userId(),
  {$set: {
     "profile.name": name,
     "profile.address": address,
     "profile.company": company,
     "profile.other": other
    }
  }
);

另请确保您的allowdeny规则允许您进行此更新。

P.S。我怀疑你的错误信息可能是因为{ multi: true }.update的第四个参数。根据{{​​3}},语法为collection.update(selector, modifier, [options], [callback]);因此,如果您想同时使用multiupsert,请将它们合并到第三个参数中的一个对象中:{ multi: true, upsert: true }(您也可以使用collection.upsert代替{.update 1}})。您的错误可能是由于您发送了一个对象{ multi: true }作为第四个参数而不是update所期望的回调函数。

相关问题