A
A
Alexander Knyazev2016-07-05 10:10:17
JavaScript
Alexander Knyazev, 2016-07-05 10:10:17

Why doesn't code work on meteor.js without checking with the check package?

There is the following code in Meteor.js - the postInsert method is called on the submit event

Template.postSubmit.events({
  'submit form': function(e) {
    e.preventDefault();

    var post = {
      url: $(e.target).find('[name=url]').val(),
      title: $(e.target).find('[name=title]').val()
    };

    Meteor.call('postInsert', post, function(error, result) {
      // отобразить ошибку пользователю и прерваться
      if (error)
        return alert(error.reason);

      Router.go('postPage', {_id: result._id});  
    });
  }
});

Here is the code for the PostInsert method:
Meteor.methods({
  postInsert: function(postAttributes) {
    check(Meteor.userId(), String);
    check(postAttributes, {
      title: String,
      url: String
    });

    var user = Meteor.user();
    var post = _.extend(postAttributes, {
      userId: user._id, 
      author: user.username, 
      submitted: new Date()
    });

    var postId = Posts.insert(post);

    return {
      _id: postId
    };
  }
});

The code works, but if you comment out the first two check functions:
/*check(Meteor.userId(), String);
    check(postAttributes, {
      title: String,
      url: String
    });*/

The code ceases to work - falls out Internal Server Error. Why is this happening? As far as I understand, check is just a check.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question