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

时间:2016-01-24 08:42:01

标签: javascript jquery node.js meteor

我正在使用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();
    });
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。我将Meteor.methods(...)移动到if(Meteor.isServer){...}块。它现在已修好。