Having trouble leaving comments on a post in meteorjs

I'm encountering an issue where I can't seem to add comments to a specific post. The comments aren't being inserted into the mongo database.

Comments = new Mongo.Collection('comments');
Template.comments.helpers({
    'comment': function(){
        console.log(this._id);
        return Comments.find();

    }
});
Template.addComment.events({
        'click button':function(event){
            event.preventDefault();
            var madeBy = Meteor.user().username;
            var comment = document.getElementById('mycomment').value;
            var currentPost = this._id;


            Comments.insert({
                comment:comment,
                createdAt:new Date(),

                madeBy:madeBy,

            });
            document.getElementById('mycomment').value='';
        }
    });

Checkout the HTML code below for the comment page:

<template name="comments">
    <h2><b>{{name}}</b></h2>

    {{> addComment}}
    <ul>
        {{#each comment}}
            <li>{{comment}}</li>
        {{/each}}
    </ul>
</template>


<template name='addComment'>
<input type='text' placeholder='Add comment here' name='comment' id ='mycomment'>
<button class="btn btn" type="button" id='btn'>Comment</button>
</template>

In the template, {{name}} is used to represent the post's name to which the comment is being added. Can someone provide assistance with this issue? Thank you.

Answer №1

It is recommended to include a form element in your addComment template;

    <template name='addComment'>
    <form class="add-Comment">
    <input type='text' placeholder='Add comment here' name='comment' id ='mycomment'>
    <button class="btn btn" type="button" id='btn'>Comment</button>
    </form> 
    </template>

In your JavaScript file:

Template.addComment.events({
  'submit .add-Comment': function(event){
   ...  
  return false;
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving form data from within the resource error callback scope

For my client-side validation on submit, I am calling a resource if the form is valid. On success, everything works fine. However, when encountering an error handler, I also perform server-side validation on my data transfer object bean which contains Hibe ...

What is the reason for JavaScript consistently returning the initial value as the result?

My current issue involves customizing Joomla article content using a module. I am attempting to hide a div until a user clicks on an input, such as a radio button labeled Test1. Once Test1 is selected, another hidden field within the div should display the ...

On iOS devices, background images may not appear in the Home Screen after being added

My CSS code looks like this: #thumbnail { background-image: url(bla.jpg), background-size: cover, background-repeat: no-repeat; background-position: 50% 50%; } While it displays fine on iOS Safari and other platforms, the image does not s ...

"Using Typescript, we can switch the keys and values in a JSON object to their corresponding values and

I have been attempting to switch keys with values and vice versa, but I haven't been able to find the correct solution using JavaScript/TypeScript. course = [ { "name" : "John", "course" : ["Java ...

Exploring intricate hexadecimal color values in three.js

While experimenting with three.js, I stumbled upon an interesting example HERE. In the init function provided in the example (only a snippet is shown here), we see: function init() { renderer = new THREE.WebGLRenderer( { antialias: true } ); ...

Constrained and Proportional Resizing with KineticJS

Dragging any of the 4 corner handles of the image should result in proportional scaling, either up or down. Issue: I am facing a problem with my current approach as illustrated in the provided link to jsfiddle. When the topLeft handles are moved verticall ...

Working with Closure in Node.js

After encountering issues with nested for loops, I decided to explore closures as an alternative solution. Below is the basic structure of my previous code: // First for loop for(data) { connection.query(records as per data) if(!error) { ...

What are the conditionals and operations that need to be underscored in this template?

I am new to working with underscore and I need help writing an "each" function for posts where only the latest post is displayed. I understand the basic logic which looks like this <% _.each(posts, function(post, index) { %> <% if(index % 3 == 0 ...

Deciphering Files with NodeJs AES Encryption and Redirecting to a Stream

I am currently working on encrypting a file in C# and decrypting it in Node.js using AES encryption. The code snippet below demonstrates the successful decryption process, however, it writes the decrypted content to an output file named `output_dec.xml&apo ...

Run the scripts that are returned from AJAX requests

Here's a helpful tip from Lucian Depold: "Instead of sending JavaScript code from PHP to JS, simply send a JSON object or array and execute each entry using the eval() function." Inside index.php, there is code that runs when the document is ready: ...

I'm sorry, but we were unable to locate the /bin/sh

After running a command using execSync that runs with sh, I observed the following: spawnSync /bin/sh ENOENT bin is now included in the PATH environment variable. Any ideas on this issue? ...

Dynamic jquery multiple select menu not refreshing properly

Welcome to the new year! I am currently working on creating a multiple select list with the attribute (data-native-menu="false"). The and elements are dynamically generated through a websql query. However, when I execute the code, none of the options are ...

Transmit data in JSON format using jQuery's AJAX function

What could be causing the PHP not to retrieve my links array? function check_links() { $matches = $this->input->get('links'); if($matches == true) { echo json_encode('matches is true'); } el ...

There was an error in the syntax: an expression was expected, but instead the character '}' was found in mongoDB

Encountering the error message SyntaxError: expected expression, got '}' @(shell):1:0 when executing a query in the shell. db.address.aggregate([ { "$project": { "applications": { "$filter": { ...

Using jquery and Ajax to extract data from nested JSON structures

Need help with modifying a code snippet for parsing nested JSON format [ { "name":"Barot Bellingham", "shortname":"Barot_Bellingham", "reknown":"Royal Academy of Painting and Sculpture", "bio":"Barot has just finished his final year at T ...

Saving a PHP array on the user's browser

I'm currently working on a website that pulls product data from a database, such as price, size, weight, etc., and displays it to the user. I am looking to enhance the user experience by adding a dropdown menu that allows users to sort the products by ...

obtain an inner element within a container using the class name in a div

I am attempting to locate a span element with the class of main-tag within a nested div. However, I want to avoid using querySelector due to multiple elements in the HTML file sharing the same class and my preference against using IDs. I realize there mig ...

Is it possible to assign a width property to a div element?

I want DivTest and IdOtherDIV to have the same width. I attempted to set properties like this: DivTest { background: #007C52; width: document.getElementById("IdOtherDIV").scrollWidth + "px.\n"; } Is there a way to achieve this (considering tha ...

"Text should be like a hidden secret, unseen yet powerful

Greetings! I have created an animation using Jquery However, I am facing an issue where the .text is visible while my info span is appearing and disappearing I need to retain that class in my .alert span In essence, what I am aiming to achieve is to mak ...

Unable to establish connection with Mongo DB

I'm encountering an issue while attempting to establish a connection to my MongoDB Atlas database. Despite seeking help on various forums related to MongoDB and Go, I've been unable to resolve the error. Here's the code snippet I'm usi ...