Iron router | how to create page views?

hi .
i many try this and this is last try.what i can do ?
i put meteor call to onbeforaction onrun and … but not resut
and how can do thit meteor.call be secure and cant run it for repeat؟

router.js

Router.route(’/g/:_id/:title’,{
name: ‘product.show’,
path : ‘/g/:_id/:title’,
layoutTemplate : ‘layout’,
onBeforeAction:function(){

  this.render('loading');
  this.next();

},
yieldRegions : {
‘home’ : {to: “main”,},
},
waitOn : function (){

  return {{........}};

},
data : function(){
return{{…}}
},

onAfterAction : function (){

  Meteor.call("number_of_visits_books", this.data().books._id  ,function(err,result){
  	if(result){
  		console.log("done")
  	}else if (err){
  		
  		console.log("err")
  	}
  })

}

});

and method.js

Meteor.methods({
number_of_visits_books: function (id) {

  console.log("run method",id)
  num =Stories.findOne({_id:id}).number_of_visits_story;	
  num +=1
  Books.update(id,{$set:{number_of_visits_story:num}});

}
})

nothing nothing result .

plz help . im confused

I’m having trouble yet ,what is solution? there is a onother way to count page views ?

sorry for spam .

In onAfterAction you’re calling a Method named number_of_visits_books, but you’ve registered a Method called number_of_visits_book (no s) via Meteor.methods. Was this just a copy/paste error?

hi tnx for answer.

in here is a typing mistake . sorry for that . i update it

actually in terminal see method run but not update this collection :frowning:

When you console.log the id param in your number_of_visits_books Method, does it look right? Also, if you console.log your num variable before trying to update it in your Books collection, does it also look right?

One small suggestion - prefix your num variable with either var, let or const. Right now you’re actually declaring num as a global variable, that can be used outside of your Method. This could lead to confusion/issues later on.

thanks hwillson

i have result correct for console.log id and num
but cant update it .
for update collection try $inc but not solved

this num variable is for test and not used elsewhere,

im confused.

Are you by chance using aldeed:simple-schema? If so are you sure the number_of_visits_story property is included in the Schema you’ve attached to Books?

yes

this is my schema

Books.attachSchema(new SimpleSchema({

number_of_visits_books :{
type : Number,
optional:true,
autoform : {
afFieldInput : {
type: “hidden”
},
afFormGroup: {
label: false
}

  },
  autoValue : function (){
  	return 0;
  }

}

}));

this is my allow

Books.allow({
‘insert’: function () {

  var loggedInUser = Meteor.user();
  if (!loggedInUser ||!Roles.userIsInRole(loggedInUser,['admin', 'publisher'])) {
  	Router.go('/404');
  	return false;
  }
  return true;

},
‘remove’: function () {
var loggedInUser = Meteor.user();
if (!loggedInUser ||!Roles.userIsInRole(loggedInUser,[‘admin’, ‘publisher’])) {

  	Router.go('/404');
  	return false;
  }
  return true;

},
‘update’:function(){

  return true;

}

});

In your Method you’re calling:

Books.update(id,{$set:{number_of_visits_story:num}})

but your schema doesn’t include a number_of_visits_story property; it includes a number_of_visits_books property. Either update your Method to use number_of_visits_books, or update your schema to use number_of_visits_story.

i’m sorry i’m sorry, this is my fault
i want to show a example .this is copy paste err,
in the my code every thing is right but cant udate

Its because of autoValue in your schema.
autoValue gets called every time the field changes, so it will override the value.

I would guess you want to have a default value for the field, you should use defaultValue for that.

1 Like

yes thats right , i want kill my self :grin:

thank you henribeck