Not able to get value from the collection in onCreated and onRendered

Hi everyone,

I am trying to get the value from the collections in onCreated and onRendered method. I am getting values when I am coming from a different page but when I reload the same page I am not getting the required results,

Either I am confused about the blaze template lifecycle or there is some issue associated with this, Please have a look at my code and help me to get the right solution


**onRelaod**

Template.grp_details.onCreated(function grp_detailsOnCreated() {
  // counter starts at 0
  	alert("onCreate");
  	var  user = Session.get("userId");
	var c = GroupUsers.find({}).count();  // Value returning 0 and it should be one
	alert(user +' time:'+c);
});

Template.grp_details.onRendered(function() {
	alert("onRedeemed");
  	var  user = Session.get("userId");
	var c = GroupUsers.find({}).count();  // Value returning 0 and it should be one
	alert(user +' time:'+c);
});

When coming from different page I am getting the right values.

**When Coming from other page**
Template.grp_details.onCreated(function grp_detailsOnCreated() {
  // counter starts at 0
  	alert("onCreate");
  	var  user = Session.get("userId");
	var c = GroupUsers.find({}).count();  // Value returning 0 and it should be one
	alert(user +' time:'+c);
});

Template.grp_details.onRendered(function() {
	alert("onRedeemed");
  	var  user = Session.get("userId");
	var c = GroupUsers.find({}).count();  // Value returning 0 and it should be one
	alert(user +' time:'+c);
});

Also, I tried to achieve the results from in the same javascript file but not getting the right solution

$("document").ready(function(){
	var user = Session.get("userId");
	var c = GroupUsers.find({"user_id":967408}).count();
	alert(user +' time:'+c);
});

Please help, Its very urgent.

1 Like

onRendered and onCreated are not reactive by default. That means they can run before the data in the collection is available on the client, and they will not re-run when it does become available.

When using Blaze, it’s expected that data can and will arrive at any time, and the correct way to present that data in your template is using template helpers. Template helpers are reactive and will re-run as often as needed whenever data changes.

Blaze HTML templates (spacebars templates) support this behaviour and ensure changes are rendered automatically.

Check out http://blazejs.org/ for more.

3 Likes

Thanks for the help @robfallows
I have a question

  • Is there any predefined method where I could achieve the desired functionality.

You can add an autorun into either of those methods to get that functionality, but it’s rather at odds with “normal” behaviour.

And what exactly is that?

Want to retrive the values from the collection when page reloads

To use where? In the HTML, or for further processing in some other logic?

I want to display whether a user is a follower of a particular group or not, So, I am getting the values from the database and checking that whether the user is already is follower of that group or not, I want to display the text on a button

Then you should use a template helper. As long as you are subscribed to all the collection data you need (it eventually becomes available on the client), that’s basically all you need to do.

1 Like

Ok. Thanks for the help @robfallows

1 Like

Incidentally, for social networking applications, you should take a look at socialize.

Definitely run through the Blaze tutorial at https://www.meteor.com/tutorials/blaze/, it’s incredibly helpful to understanding the data flow and lifecycle of Blaze templates and Meteor.

Also worth reading this, which explains how, where and why you need to handle data

And as an additional nit, you should use Meteor.userId() instead of Session as the session can be altered from the console and is not secure.

1 Like

Thanks @robfallows, I will surely look into it

Thanks, @coagmano will definitely read this

1 Like