Exception while simulating the effect of invoking 'addPost', is this normal?

#1

Is it normal to get this message in the browser console when throwing an Meteor.Error from a Meteor.methods?

Exception while simulating the effect of invoking ‘addPost’

The code looks like this:

/lib/posts.js

Meteor.methods({
 addPost: function (post) {
   if (postExists(post.name, post.slug)){
     throw new Meteor.Error(500, 'Post already exists');
   } 
  
   var postId = Posts.insert({
     name: post.name,
     slug: post.slug
   });

   return {_id: postId}; 
  }
})

/client/addPost.js

Meteor.call('addPost', post, function(error, result){
  if (error) {
    alert(error, error.reason);
  } else {
    Router.go('editPost', {_id: result._id});
  }
});

Shouldn´t it be handled by the Meteor.call callback function? Is this a debug level issue? I’ve seen this pattern in lots of examples.

Other approach would be to return instead of an error a result.error and handle it on the client, but I feel like throwing an error makes more sense, isn´t it?

Thanks!

#2

Hi,
I am having the exact same issue. Can you please let me know if you got answer to your question?

1 Like
#3

Yes, this is a normal error you might encounter when playing around with client/server methods. Even though it’s normal, it’s because you’re managing your Meteor methods incorrectly, and the error is thrown on the Client side method, while I’m guessing your method completes.

The most common cause of such error is calling a client or client/server method where the data you’re asking for is not available, such as postExists(post.name, post.slug) function or Posts.insert not being available on client either by definition or by allow/deny rules you use.

My recommendation is to keep it in the server side only (check your folder structure to sort this out).

An example I used to encounter is retrieving User.findOne().profile in a method called on client side, where User.collection was not published, that caused minimongo to try to solve Client facing method by .User.findOne(id) to return undefined, and hence undefined.profile threw an error.
I could have done the following, which will apply to you:

  1. Publish Users to Client side to populate miniMongo and find it in simulation
  2. Define a user = Users.findOne(); if(user && user.profile){return user} else {return null} to avoid running into an exception on simulation.
  3. Move the method to Server side only, hence no Method “simulation” runs.

It’s important to know that #2 will never override the result obtained from server side, it will trigger correctly, just not eagerly (or optimistically as you might call it).
Also, I chose #3 because I didn’t want to publish Users to front end, as I consider it sensitive information.
Choose the best configuration for your scenario, and consider that #2 will work well when you want optimistic UI, If the first page you load triggers this method before Minimongo is populated (even though you have your data there published to Posts) it will not be optimistic, but the second and next calls will be, as Minimongo will have data in it.

Cheers! @helios and @vjain27

1 Like