gpt4 book ai didi

javascript - 为什么模板事件中的 findOne 会导致调用堆栈大小错误?

转载 作者:行者123 更新时间:2023-12-03 09:19:23 24 4
gpt4 key购买 nike

我试图在 SpaceBars 中添加一个 forEach 来显示一些可以为数组中的每个内容按下的按钮。但是,当我按下按钮时,它给出了这个错误:

Uncaught RangeError: Maximum call stack size exceeded underscore.js:1025
_.(anonymous function) @ underscore.js:1025
EJSON.clone @ ejson.js:475
(anonymous function) @ ejson.js:494
.each..forEach @ underscore.js:113
EJSON.clone @ ejson.js:493

... same stuff for a while ...

.each..forEach @ underscore.js:113
EJSON.clone @ ejson.js:493
(anonymous function) @ ejson.js:494
.each..forEach @ underscore.js:113
EJSON.clone @ ejson.js:493

这是导致错误的 findOne:

"click .acceptFriend": function(requester){
currentUserId = Meteor.userId();
requesterUsername = requester;
requesterId = friendRequests.findOne({$and:[{target:currentUserId}, {requester:requesterUsername}]})._id;

Meteor.call("acceptRequest", requesterId, requester);
}

我知道这是 friendRequests.findOne 因为我已经在每一行上完成了控制台日志,而这就是中断它的行。我尝试了几种不同的变体,但似乎在该上下文中对 friendRequests 集合进行任何类型的查找都会破坏它。模板的相关部分是这样的:

{{#each getRequests}}
{{requester}} <button class='acceptFriend'>accept</button>
{{/each}}

为什么会发生这个错误以及如何解决它?

最佳答案

如果我们看 the documentation about events :

The handler function receives two arguments: event, an object with information about the event, and template, a template instance for the template where the handler is defined. The handler also receives some additional context data in this, depending on the context of the current element handling the event. In a template, an element's context is the data context where that element occurs, which is set by block helpers such as #with and #each.

总结: Meteor 中的事件接收其数据上下文作为参数:它们接收事件对象和事件>事件发生的模板实例。数据上下文存储在 this 中。

因此,您可能应该执行以下操作:

"click .acceptFriend": function(event, template){
currentUserId = Meteor.userId();
requesterUsername = this.requester;
requesterId = friendRequests.findOne({$and:[{target:currentUserId}, {requester:requesterUsername}]})._id;

Meteor.call("acceptRequest", requesterId, requesterUsername);
}

关于javascript - 为什么模板事件中的 findOne 会导致调用堆栈大小错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com