gpt4 book ai didi

javascript - Meteor.call(...) 导致刷新页面

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:17 25 4
gpt4 key购买 nike

我正在使用 Meteor 开发一个非常简单的民意调查应用程序。

voteapp.meteor.com

点击投票按钮(选择选项后出现)后,页面刷新。

点击事件:

    Template.poll.events({
"click input.inc": function (event) {
// Prevent default browser form submit
event.preventDefault();

Meteor.call('voteForAnswer', Session.get("selectedanswer"), function (error, result) {
if (error) {
Session.set("voteError", error.reason);
}
else {
Session.set("voteError", null);
Session.set("selectedpoll", result);
}
});
}
});

Meteor.call函数:

'voteForAnswer': function (id, vote) {
var answer = Answers.findOne(id);
var poll = Polls.findOne(answer.poll);
if (poll && answer) {
Answers.update(answer, { $inc: { score: 1 } }, function (error) {
if (error) {
throw new Meteor.Error(411, "Answer could not be updated.");
}
});
Polls.update(poll, { $inc: { sum: 1 }, $set: { lastActivity: new Date() } }, function (error) {
if (error) {
throw new Meteor.Error(411, "Poll could not be updated. " + poll.name);
}
});

poll = Polls.findOne(poll._id);
var answers = Answers.find({ poll: poll._id }).fetch();
answers.forEach(function (ans) {
ans.score && Answers.update(ans, { $set: { percent: Math.round((parseInt(ans.score) / parseInt(poll.sum)) * 100) } });
});
poll.answers = Answers.find({ poll: poll._id }, { sort: { score: -1 } }).fetch();
}
return poll;
}

另外:

if (Meteor.isClient) {

Meteor.subscribe("polls");
Meteor.subscribe("answers");

和:

if (Meteor.isServer) {
Meteor.publish("polls", function () {
return Polls.find();
});
Meteor.publish("answers", function () {
return Answers.find();
});
}

最佳答案

我发现了问题。我将 Meteor.methods(...) 移至 if (Meteor.isServer) { ... } block 。现在已经修复了。

关于javascript - Meteor.call(...) 导致刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34973760/

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