问题
When validating objects for database insert, I would like to access the Meteor.users collection to let the user know if he added an event manager to the event who is stored in the database. I extended the discover meteor book example for my needs. I know of the Meteor Accounts API, but I'm stuck at the moment how to proceed.
Here is the Meteor method
Meteor.methods({
eventInsert: function(eventAttributes) {
check(Meteor.userId(), String);
check(eventAttributes, {
title: String,
shareInPercent: Number,
eventDate: Date
});
var errors = validateEvent(eventAttributes);
if (errors.title || errors.shareInPercent || errors.eventDate)
throw new Meteor.Error('invalid-event', "Check your inputs again.");
var user = Meteor.user();
var event = _.extend(eventAttributes, {
userId: user._id,
author: user.username,
submitted: new Date()
});
var eventId = Events.insert(event);
return {
_id: eventId
};
}
});
validateEvent = function (event) {
var errors = {};
if (!event.title)
errors.title = "Please fill in an event title";
if (!event.eventDate)
errors.eventDate = "Please enter a date";
var eventManagersNotExisting = areEventManagersExistingUsers(event.eventManagers);
return errors;
}
areEventManagersExistingUsers = function (eventManagers) {
var eventManagersNotExisting = [];
_.each(eventManagers, function(eventManager){
var eventManagerFound = Meteor.users.findOne({username: eventManager});
});
return eventManagersNotExisting;
}
When an insert is triggered, the Meteor method is called, the validateEvent method is called in-between, which calls itself a helper method to check the users collection as seen here
var eventManagerFound = Meteor.users.findOne({username: eventManager});
If I'm right and because it should make sense, I can't put console.log()
messages in the Meteor method, because the method is executed server side and the message won't appear in the browser console.
My first question therefore is, is there a way to look at log messages done on server side, similar to Node, where these messages just appear in the shell?
I read about the meteor debug
and meteor shell
command, but I'm not seeing any log messages appearing there, which I put into code.
Log messages in validateEvent
and areEventManagersExistingUsers
appear in the browser though.
Now my second question, when logging messages in validateEvent
and areEventManagersExistingUsers
, why do they show up in the browser console?
And the result of
var eventManagerFound = Meteor.users.findOne({username: eventManager});
is always undefined, only when eventManager is my own user, it's finding that entry.
But is it working for the Meteor.method call, and I'm seeing only a side effect of the proper execution or do I make something wrong here conceptionally?
Edit:
As said in the commentary below, to fix the console.log() when executing the Meteor method, I first had to activate preserve log in the Chrome console, because the form submit is refreshing the site so fast, I just didn't see that the site was reloaded and the log erased.
But a requirement to see the log in the browser console, is the need to have a successful submit (i.e., I had to fill all fields correctly), for that I fixed the check
check(eventAttributes, {
title: String,
shareInPercent: Number,
eventManagers: [String],
eventDate: Date
});
But a log of Meteor.users.find();
only returns my user and not all users in the database. Any idea how to get all of them without publishing the users collection?
来源:https://stackoverflow.com/questions/29922708/how-to-access-meteor-user-collection-on-server-and-debug-correctly