I have a requirement where I only want to allow users to insert a document if their email is verified. In an attempt to achieve this, I wrote the following code snippet.
Events.allow({
insert: function (userId, doc) {
var user = Meteor.users.find({_id: userId});
console.log(user); // displays a large object
// check if the user's email is verified
if (user.emails[0].verified) {
return true;
}
return false;
}
});
Upon running this code, I encountered an "internal server error" and on further investigation on the server side, I got a
TypeError: Cannot read property '0' of undefined
, suggesting that there is an issue with my user object. To debug this, I logged the object and instead of seeing the expected user data, I was presented with a massive object structure as shown below:
{ _mongo:
// myriad details
}
It appears that this is the top level mongoDB object utilized by Meteor.
The perplexing part is how Meteor.users.find({_id: userId})
returned the MongoDB object rather than the specific user I intended to retrieve?