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);
});
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.
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.
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.