【问题标题】:Why do I get the Maximum call stack size exceeded error. Without .val()为什么我会收到超出最大调用堆栈大小的错误。没有 .val()
【发布时间】:2014-08-27 02:08:48
【问题描述】:

当我尝试使用关注按钮时,我正在尝试创建一个关注功能,我得到“未捕获的 RangeError:超出最大调用堆栈大小”如果我将 .val() 添加到 Meteor.users.update(Meteor.userId(), {$addToSet: {'profile.following': usern}}); 的末尾,它会返回 " "profile.following 它应该返回用户所在配置文件的用户名。提前致谢,这里是代码。

    Template.user_profile.events({
     'click #follow':function () {
        var usern=$("#username");
        Meteor.users.update(Meteor.userId(), {$addToSet: {'profile.following': usern}});
        return Meteor.user().profile.following;
    }
})

html

  <template name="user_profile">
  <h2 id="username">{{username}}</h2>
  <div>{{profile.followingnumber}}</div>
  <div>{{profile.followedbynumber}}</div>
  <div>{{profile.bio}}</div>
  <button id="follow">Follow</button>
</template>

服务器端

Accounts.onCreateUser(function(user) {
    user.profile.following=[];
    user.profile.followingn=0;
    user.profile.followed=[];
    user.profile.followedn=0;
    return user;
});

【问题讨论】:

    标签: javascript jquery html meteor


    【解决方案1】:

    在事件处理程序中,您可以使用this 访问当前数据上下文,因此不要通过jQuery 访问用户配置文件用户名(您应该使用$("#username").text().val() 仅用于表单元素! ),只需使用this.username

    所以这就是你应该想出的:

    Template.user_profile.events({
      "click #follow":function(){
        Meteor.users.update(Meteor.userId(),{
          $addToSet:{
            "profile.following": this.username
          }
        });
      }
    });
    

    在此点击事件中没有特别需要返回当前用户关注列表,首先不确定您的意图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2011-08-31
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多