【问题标题】:.update Not working -- Meteor.update 不工作——流星
【发布时间】:2014-09-11 05:49:39
【问题描述】:

出于某种奇怪的原因,我的 .update 函数无法正常运行。我认为this._id 的选择器是这里的问题,但我尝试传入数据库中一个项目的原始 ID。使用good_input.insert 的内容全部存储在数据库中,但.update 只是跳过查看结果。

 Template.newInstitution.events({
    'click #form_institution': function () {
      var inst_name = $('#name')[0].value;
      var inst_type = $('#type')[0].value;
      var inst_school = $('#school')[0].value;
      var inst_desc = $('#description')[0].value;
      var good_input = true;
      if (inst_name === "") {
        $('#name')[0].placeholder = "Please enter a name";
        $('#nameDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_type === "") {
        $('#type')[0].placeholder = "Please enter a type";
        $('#typeDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_school === "") {
        $('#school')[0].placeholder = "Please enter your affiliated school";
        $('#schoolDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_desc === "") {
        $('#description')[0].placeholder = "Please enter a description";
        $('#descDiv').addClass("has-error");
        good_input = false;
      }
      if (good_input) {
        Institutions.insert({
          admin: Meteor.userId(),
          title: inst_name,
          type: inst_type,
          school: inst_school,
         description: inst_desc
        });
        Institutions.update({_id: this._id},
          {$push: {
            members: Meteor.userId()
          }}
        );
        Router.go('songs');
      }
    }
  });

https://bpaste.net/show/befaba419c19

有人会在这里找出问题并解释原因吗?

【问题讨论】:

标签: javascript jquery mongodb meteor


【解决方案1】:

您认为this._id 设置在哪里?除非有一些不可见的进一步上下文,否则没有这样的事情。虽然我同意您应该只在初始 insert 中包含数据,但您可能正在寻找的是 var id = Institutions.insert({...}) 并使用 id 而不是 this._id

【讨论】:

  • 是的。知道了。实际上回答了我自己的问题:)
【解决方案2】:

这就是修复。基本上,我可以在原始语句中设置一个数组,稍后使用 $push。

  Template.newInstitution.events({
    'click #form_institution': function () {
      var inst_name = $('#name')[0].value;
      var inst_type = $('#type')[0].value;
      var inst_school = $('#school')[0].value;
      var inst_desc = $('#description')[0].value;
      var good_input = true;
      if (inst_name === "") {
        $('#name')[0].placeholder = "Please enter a name";
        $('#nameDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_type === "") {
        $('#type')[0].placeholder = "Please enter a type";
        $('#typeDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_school === "") {
        $('#school')[0].placeholder = "Please enter your affiliated school";
        $('#schoolDiv').addClass("has-error");
        good_input = false;
      }
      if (inst_desc === "") {
        $('#description')[0].placeholder = "Please enter a description";
        $('#descDiv').addClass("has-error");
        good_input = false;
      }
      if (good_input) {
        Institutions.insert({
          admin: Meteor.userId(),
          title: inst_name,
          type: inst_type,
          school: inst_school,
          description: inst_desc,
          members: [Meteor.userId()]
        });
        Router.go('songs');
      }
    }
  });

【讨论】:

    猜你喜欢
    • 2016-08-02
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多