How can verify the Meteor.users collection is ready?
I have some code setup to run on Meteor (Release 0.7.0.1) startup on the client that is not behaving correctly because the Meteor.users collection isn't populated yet. In this code Meteor.users._connection.status().connected
returns true and DDP._allSubscriptionsReady()
returns false so the Meteor.users.findOne
call returns nothing even though it should have returned the user.
Meteor.startup(function() {
Deps.autorun(function () {
var userTracker = PersistentSession.get('userTracker');
var userTrackerMissing = !userTracker;
var anonymousUserMissing = !Meteor.users.findOne({"_id": userTracker});
if (Meteor.users._connection.status().connected) {
if (DDP._allSubscriptionsReady()) {
if (userTrackerMissing || anonymousUserMissing) {
Meteor.loginAnonymously();
}
}
}
});
});
You might try using fast-render
package. It makes Meteor.users
collection to be populated from the start. Run:
mrt add fast-render
Check out this detailed article on how it works.
I was able to resolve this using standard Meteor facilities like this. On the server I published users like so.
Meteor.startup(function () {
Meteor.publish("userData", function(userTracker) {
var user = Meteor.users.find({_id: userTracker}).fetch();
return Meteor.users.find({_id: userTracker},
{
fields:
{
'votes': 1
}
}
);
});
});
On the client I did the following to check if the published userData was available yet.
Meteor.startup(function() {
Deps.autorun(function () {
var userTracker = PersistentSession.get('userTracker');
var userTrackerMissing = !userTracker;
var userReady = Meteor.subscribe('userData', PersistentSession.get('userTracker')).ready();
if (userReady) {
var anonymousUserMissing = !Meteor.users.findOne({"_id": PersistentSession.get('userTracker')});
if (userTrackerMissing || anonymousUserMissing) {
Meteor.loginAnonymously();
}
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.