Problem adding comments to a mongo collection


#1

Hey guys, so I’ve been trying to add comments to one of my mongo collections. But I’m unable to get them to display. I don’t get any errors on the console, but I’m unable to debug. Any help will be greatly appreciated…

Here is a GIST with the pertaining code.

Here is the code, from the gist:

publish.js

… ommitted code

// All Fritkots

Meteor.publish(‘fritkots’, function() {
return Fritkots.find( { author: this.userId } );
});

// Single Fritkot
Meteor.publish(‘singleFritkot’, function(id) {
check(id, String);
return Fritkots.find( { _id: id } );
});

Meteor.publish(‘fritkotComments’, function(fritkotId) {
// check(fritkotId, String);
return Comments.find( { fritkotId: fritkotId }, { sort: { timestamp: -1}, limit: 10 });
})

Meteor.publish(‘comments’, function() {
return Comments.find({ author: this.userId}, { sort: { timestamp: -1 }, limit: 20 });
});

fritkotSingle.js

import ‘./fritkotSingle.tpl.jade’ // Single Fritkot View Page

// On Creation
Template.fritkotSingle.onCreated(function() {
let self = this;

self.autorun(() => {

  let fritkotId = FlowRouter.getParam('fritkotId');
  self.subscribe('singleFritkot', fritkotId);
  self.subscribe('fritkotComments', fritkotId);

console.log(params of the subscription from the fritkotSingle.js are ${fritkotId})
})
});

// Helpers
Template.fritkotSingle.helpers({
fritkot: () => {
let fritkotId = FlowRouter.getParam(‘fritkotId’);
let fritkot = Fritkots.findOne( { _id: fritkotId } ) || {};

  console.log(`The current fritkotId is: ${fritkotId}`);
  console.log(`The current fritkot is: ${fritkot}`);
  return fritkot

},
editable: function () {
return Session.equals(‘editFritkotId’, true)
},
// comments: function() {

// // let fritkot = Fritkots.findOne( { id: Session.get(this._id)} );
// let fritkotId = FlowRouter.getParam(‘fritkotId’);
// return Comments.find( { fritkotId: fritkotId } ) || {};
// },
// commentsCount: function(fritkotId) {
// return Comments.find( { fritkotId: fritkotId} ).count();
// console.log(There are ${Comments.find( { fritkotId: this._id} ).count()} comments written by this user);
// },
// fritkotComments: function(fritkotId) {
// return Comments.find( {fritkotId: fritkotId }, { sort: { timestamp: -1 } });
// }
comments: function () {

  let fritkotComments = Comments.find( { fritkotId: this._id } )
  console.log(`Comments for this fritkot: ${fritkotComments}`);
  return fritkotComments

},
commentsCount: (fritkotId) => {
return Comments.find( { fritkotId: fritkotId } ).count() || ‘No data’;
}
});

fritkotSingle.tpl.jade

with fritkot
if editable
+editFritkot
else
.section.section–single
article.card.card–single
h3.card__title= name
p.card__content{{commentsCount}} comments
p.card__content= fritkot.comment
p.card__content= address
p.card__content
= fritkot.gemeente
span.card__content= postCode
p.card__info= info
if fav
button.card__btn.btn–isDenied.toggle-fav Remove from Favorites
else
button.card__btn.btn–isAllow.toggle-fav Add to Favorites
//- button.card__btn.toggle-fav Add to Favorites
button.card__btn.form-delete Delete
//- button.card__btn.btn–isEdit.form-edit Edit
button.card__btn.form-ecdit Edit
.section.section–comments
h3.section__title This is the comments section
h3.section__subtitle Start Comenting
if $.Session.get ‘createComment’
+createComment
else
button.btn.form__btn.add-comment Comment!
.section.section–comments
each comments
p= body

createComment.js

import ‘./createComment.tpl.jade’

Template.createComment.events({
‘click .form-save’: (e) => {
e.preventDefault();

  let commentContent = $('.new-comment').val();
  let fritkotId =  Fritkots.findOne({})._id;
  Meteor.call('createComment', commentContent, fritkotId);
  console.log(`A comment was inserted with this body: ${commentContent}`);
  Session.set('createComment', false);

},
‘click .form-close’: (e) => {
e.preventDefault();

  Session.set('createComment', false);

}
})

createComment.tpl.jade

form.form
input.new-comment.focus(type=‘text’, placeholder=‘Let’s hear it’)
button.btn.form__btn.form-save Submit
button.btn.form__btn.form-close Close


#2

I have EXACTLY The same problem (cf here)

I tried 2 hours to fix it yesterday. Nothing more.
So if anyone can help us, it would be great.


#3

Did you fix your problem?


#4

Just solved the problem, problem was with how was defining my Comments.collection with the SimpleSchema. Got it to work by commenting it out, but will add the code once I make it work.

But here is the collection/comments.js as it stands now

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

// Methods
Meteor.methods({
// updateComment: (id) => {
// Comments.update(id, {
// $set: { text: }
// }),
createComment: function (commentContent, fritkotId) {
check(commentContent, String);
check(fritkotId, String);

  let currentUserId = Meteor.userId();
  if(currentUserId && fritkotId) {
  	Comments.insert({
  		body: commentContent,
  		author: currentUserId,
  		fritkotId: fritkotId
  	});
  } else {
  	throw new Meteor.Error('you must comment on a fritkot');
  }

},
updateComment: (id) => {
// Session.set(‘editComment’, true);
},
deleteComment: (id) => {
Comments.remove(id);
}
})