Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:03:24+00:00 2026-06-15T19:03:24+00:00

I have a problem when running with Meteor. I have a question page which

  • 0

I have a problem when running with Meteor.

I have a “question” page which I want to increase the count view whenever it is rendered.

So in my template function I write

Template.questionview.helpers({
    question : function() {
      if(Session.equals('main_template_name', 'question')) {
        console.log(Session.get('question_id'));
        Questions.update({
          _id: Session.get('question_id')
        }, {
           $inc: {
           views: 1
        }
     });
   }
});

Now here comes the problem, when I render the question view and update the question item, the view is refreshed again because it is a reflective page. And then it comes infinity loop.

Anyone has suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:03:25+00:00Added an answer on June 15, 2026 at 7:03 pm

    Typically, in situations like this, there is something broken about the model. In this case, I believe it’s the “count view” idea. There are lots of ways to do this correctly. Incrementing it on render is not, since you’re doing model work in UI code (broken conceptually and in implementation).

    First, store the questions the user has visited somewhere. Why not a {questionsVisited:[]} property on the user?

    Use a Meteor.call(...) method call to register a view instead:

    Meteor.methods({
      viewQuestion: function(questionId) {
        // check if the user hasn't visited this question already
        var user = Meteor.users.findOne({_id:this.userId,questionsVisited:{$ne:questionId}});
    
        if (!user)
             return false;
    
        // otherwise, increment the question view count and add the question to the user's visited page
        Meteor.users.update({_id:this.userId},{$addToSet:{questionsVisited:questionId}});
        Questions.update({_id:questionId},{$inc:{views:1}});
        return true;
    });
    

    So how about incrementing the view on UI changes? Well, let’s not do that specifically. Let’s increment the view count only when the question changes.

    Meteor.autorun(function () {
      var questionId = Session.get("question_id");
      Meteor.call('viewQuestion',questionId,function(e,r) {
        if (r)
          console.log("Question " + questionId + " logged an increment.");
        else 
          console.log("Question " + questionId + " has already been visited by user " + Meteor.userId();
      });
    });
    

    And get rid of all this question helper stuff…

    This is even better than what you originally wanted. Now views aren’t counted twice for the same user. If that is the desired behavior, remove the questionsVisited logic.

    Only change the 'question_id' session variable when you actually change the logical question the user is working with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the problem running executable .jar file. I've created a project which contains
I have a problem in running a new project which I downloaded from github.
I have a problem running an app on iOS 5 which works fine on
I have a problem running another file from php. I want my php params
I have got a problem about running codedui test which was generated from manual
As the question says, I have a problem running the web app on local
I have a problem running my jsp pages. My jsp pages has reference to
I have a problem running an insert query via C# OleDbCommand to an Access
I'm a beginner with jdbc ... I have a problem running this code :
I have a problem with running jquery: <!DOCTYPE html> <html> <head> <meta http-equiv=Content-Type content=text/html;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.