0

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?

Community
  • 1
  • 1
Michael K.
  • 535
  • 6
  • 21
  • 1
    Any `console.log` in the server code will show up in the console where you started meteor. So yes, you can absolutely use that server-side. – Christian Fritz Apr 28 '15 at 15:03
  • Ok, but a `console.log` following `eventInsert: function(eventAttributes) {`, does not yield a message in the browser. Any idea why? – Michael K. Apr 28 '15 at 16:37
  • `eventInsert` is a method, i.e., it runs on the server, not the browser – Christian Fritz Apr 28 '15 at 18:52
  • So when you say that logs in server code show up in the console, which console do you mean? I don't see them in the terminal at least. – Michael K. Apr 28 '15 at 20:18
  • which OS are you running? in linux and osx you will see any `console.log` output on the server side in the terminal where you started meteor. – Christian Fritz Apr 28 '15 at 21:19
  • OSX with Terminal at home and Windows 8 with Power Shell at work. But you gave me an idea, how to google the correct question and I found this StackOverflow -> http://stackoverflow.com/questions/14346394/why-is-console-log-not-printing-anything Because it's a form, "preserve log" in the browser console needs to be checked, the messages appear in the console now. I also had to fix the check, and the log only appears when the submit is successful. Because I tried to let it crash during valdiateEvent. Nevertheless, `Meteor.users.find()` only returns my user, not all users, despite being on server – Michael K. Apr 29 '15 at 06:41

0 Answers0