Problems when updating objects from the client with a Meteor.call('my_method')


#1

Hey peeps, I’m trying to update some records from the client by doing a
Meteor.call('updateComment'...), but I keep getting a server error on the client side.

Could someone please point me in the right direction?

Thanks in advance.

Full-Error

errorClass {error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", errorType: "Meteor.Error"}

editComment.js

      // Events
   Template.editComment.events({
        'click .form-save': (e)  => {

	e.preventDefault();

	let commentContent = $('.update-comment').val();

	let fritkotId =  Fritkots.findOne({})._id;
	let commentId = Comments.findOne({})._id;

	Meteor.call('updateComment', commentContent, commentId, fritkotId, function(error) {
		if (!error) {
			console.log('comment updated');
		} else {
			console.log(error);
			console.log('unable to update comment');
		}
	})

	console.log(`The comment was updated to this ${commentContent}`);
	
	console.log('I was clicked for save');
	Session.set('editCommentId', null);

server/comments/publish.js

updateComment: function (commentContent, commentId, fritkotId) {
	let currentUserId = Meteor.userId();
	Comments.update({
			body: commentContent,
			author: currentUserId,
			fritkotId: fritkotId,
			createdAt: new Date()
		});
}

collections/comments.js

// Permissions
Comments.allow({
	insert: () => false,
	// update: (userId, doc) => { return !! userId; },
	update: () => false,
	remove: () => false
});

Comments.deny({
	insert: () => true,
	update: () => true,
	// update: (userId, doc) => { return !! userId; },
	remove: () => true
});

#2

I don’t see that you have your server code in a Meteor.methods block:

Meteor.methods({
  updateComment(commentContent, commentId, fritkotId) {
    let currentUserId = Meteor.userId();
    Comments.update({
      body: commentContent,
      author: currentUserId,
      fritkotId: fritkotId,
      createdAt: new Date()
    });
  }
});

Also, a point about editComment.js: You need to move your “comment was updated” inside the success part of the callback - that section of code will always be executed immediately, regardless of whether the method call has finished or not:

    Meteor.call('updateComment', commentContent, commentId, fritkotId, function (error) {
      if (!error) {
        console.log('comment updated');
        console.log(`The comment was updated to this ${commentContent}`);
      } else {
        console.log(error);
        console.log('unable to update comment');
      }
    });

Incidentally, it’s usually better to use a template ReactiveVar than a global Session variable. Check out the ReactiveVar doc and declaration in Blaze.