Comment section of a web app not woking


#1

Hello,
I am new to meteor and In my web app (Meteor 1.2.1) the users can make posts and under the posts can make comments. I am pushing the comments into the comment array in the post collection but the code updates the array only when me as the owner of post i make a comment in my own post. If i try to make a comment in someone’s else post the array updates with an empty string. The second is that i want to update the commentUsers array which holds the user who made a comment. This works but it updates the array with all the information of the user, i just want the username. And the third is i don’t know how to access the comments array of a certain post and display the results on the screen.
Here is the code:

postComments.js:

Template.commentForm.events({

  'submit .comment-form': function(event){
    event.preventDefault();
    var content = document.getElementsByClassName('form-comment')[0].value;
    console.log(content);
    var user = Meteor.user();
    var username = user.username;
    console.log(username);
    Meteor.call('comment', this, content, user);

  }
});

methods.js:

Post = new Mongo.Collection('post');

Meteor.methods({
  addPost: function(content){
    if(!Meteor.userId()){
      throw new Meteor.Error('not-authorized', 'you are not signed');
    }
    var username = Meteor.user().username;

    Post.insert({
      content: content,
      created: new Date(),
      username: username,
      comments: [],
      commentUsers: []
    });
  },
  follow: function(post){
    console.log(post);
    var user = Meteor.user();

    if(!Meteor.userId()){
      throw new Meteor.Error('not-authorized', 'you are not signed');
    }

    if(user.username != post.username && user.profile.follow.indexOf(post.username) == -1) {
      //if everything is correct then we will update the current user and will push
      //the to the array the new user name which we are taking his information from the post
      Meteor.users.update({_id: user._id},
        {$push: {'profile.follow': post.username}
      });
    }
  },
  comment: function(post, content, username) {
    console.log(post);
    console.log(content);
    //var user = Meteor.user();

  if(!Meteor.userId()){
    throw new Meteor.Error('not-authorized', 'you are not signed in');

  }

  Post.update({_id: post._id},
    {$push: {'comments': content, 'commentUsers': username }}
    //{$push: {'commentUsers': username }}

  );
}
})

commentList.js:

Template.commentsList.helpers({
  comments: function(postId){
    var comments = Post.findOne({postId: postId})._id;

    return  comments.comments;
  }
});

commentList.html:

<template name="commentsList">
  <ul>
    {{#each comment in comments}}
    <li>{{comments _id}}</li>
    {{/each}}
  </ul>
</template>

#2

You might try these packages.

https://atmospherejs.com/socialize


#3

This should just be

{{#each comments}}
  {{_id}}
{{/each}}

Your syntax for {{#each}} was wrong.

This will work if a content is an object that looks like {_id: "blah", message: "message"}.


#4

Actually it was also a correct syntax but needed to reference the new comment var inside the “#each…in” block


#5

Ah, I wasn’t sure if that existed in Meteor 1.2.1 :slight_smile: Good catch!


#6

Thanks for the answers. I uploaded an image of my browser console to show you what i get when i post a comment. The comment array of the last post is filled with comments while the rest have empty strings.