enhance mongodb collection with meteor's capabilities

Looking for assistance with my Meteor app. I currently have a functionality to list names and numbers, insert and delete them. Now, I want to add a button to change either the name or number field. How can I achieve this in Meteor?

Below is a snippet of my code:

.html

 <head>
    <title>Meteor_CRUD</title>
</head>

<body>
    {{> index}}
</body>

<template name="index">
    <h1>List</h1>
    <table> 
        <thead>
            <td>Name</td>
            <td>Number</td>
        </thead>
        <tbody>
            <tr>
                <td><input class="name" type="text"/></td>
                <td><input class="number" type="text"/></td>
                <td><button class="add">add</button></td>
        </tr>
        {{#each list}}
            <tr>
                <td>{{name}}</td>
                <td>{{number}}</td>
                <td><button class="del">del</button></td>
                <td><button class="change">change</button></td>
            </tr>
        {{/each}}
    </tbody>
</table>

.js

List = new Meteor.Collection("list");

if (Meteor.isClient) {

Template.index.helpers({
    list : function () {
        return List.find();  
    }
});

Template.index.events({
    'click .del' : function (evt, tmpl) {
        List.remove(this._id);
    },

    'click .add' : function (evt, tmpl) {
        var name = tmpl.find(".name").value;
        var number = tmpl.find(".number").value;

        List.insert({name: name, number: number});

        tmpl.find(".name").value = '';
        tmpl.find(".number").value = '';
    },

    'click .change' : function (evt, tmpl) {
        //List.update(this._id);
    }
});
}

if (Meteor.isServer) {
   Meteor.startup(function () {
      // code to run on server at startup
   });
}

Need some help with this task!

Thank you!

Answer №1

To modify the field, start by locating the document using a specific field (such as _id), then apply the changes to the document as shown below

'click .change' : function (evt, tmpl) {
        //List.update(this._id);
         var name=this.name;
         var number=this.number;
         List.update({_id:this._id},{$push:{name:name,number:number}});
    }

Ensure that you have the necessary permissions to update the collection by adding the following code on the server side

List.allow({
            update:function(userId, doc, fields, modifier)
              {
            //anyone can update the collection
            //you can write some filters to restrict the updating to only owner of the document

               return true;
              }
        });

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

When running nodemon in an npm project without any arguments, it fails to execute the value specified in the "main" field of the package.json

My current npm project has the following folder structure: project ├── node_modules │ └── (node_modles folders) ├── server.js ├── index.js ├── index.html ├── package.json └── package-lock.json The JavaScr ...

I attempted to make changes to a Firebase document using the update() function, however, the document did not reflect

I am currently in the process of creating a blog and have been focusing on implementing an edit post feature. However, I have encountered an issue where when I utilize the ref.update() method, it indicates that the update was successful but I do not see an ...

Tips on aligning a div to the right of a headline

My goal is to have an orange circle image placed to the right of my headline <h2>. However, I am facing an issue where the circle jumps above the headline when I shrink the screen. How can I ensure that it stays on the right side no matter the screen ...

Trigger an endless loop upon window.onload event

Every now and then, when a user opens the page, it starts flickering and resizing itself. After checking the log, it appears that the submit function is being called multiple times, but I can't reproduce this issue in my own environment. Is there som ...

Generating a single JSON record in React Native

Need help displaying a single JSON record from an API request on the screen. const [user, setUser] = useState(); const getUserData = async () => { // {headers: {Authorization: "Basic " + base64.encode(username + ":" + passwor ...

Utilizing jQuery to Bind AJAX Events without a DOM Element

The documentation for jQuery AJAX Events provides examples that all involve using a jQuery DOM Element to declare a binding, like so: $('.log').ajaxSend( hander ); I am interested in capturing jQuery AJAX Events without the need for a DOM Eleme ...

Looking for assistance in showcasing information retrieved from an external API

I've been working with an API and managed to fetch some data successfully. However, I'm having trouble displaying the data properly in my project. Can anyone provide assistance? Below is a screenshot of the fetched data from the console along wit ...

Is it possible to add a sub-document to a specific parent document by using the parent's ID and providing the name:value pair for

Is there a way to update an embedded document by finding the parent using the _id? I need help with the correct syntax for locating the parent by _id and the sub-document by the username field: Models.Message.findById(req.params.message_id, { "to.usernam ...

Leveraging JSON Array in HTML with Ionic 2

I am encountering difficulty extracting information from an array of JSON objects called 'books' for use in my HTML. The structure of the array is as follows: https://i.sstatic.net/25aqG.png Each object follows this format: https://i.sstatic. ...

My Jquery "animate" is only triggered once instead of on each function call. What could be causing this issue?

I have set my navigation to dock at a specific document height on the top of the viewport, and when it docks, it should display a dropdown animation. $(document).scroll(function(){ var x = $(window).scrollTop(); if(x>=700){ $("header ...

Is there a way to adjust the timepicker value directly using the keyboard input?

Is there a way to control the material-ui-time-picker input using the keyboard instead of clicking on the clock? This is my current code: import React, { Component } from "react"; import { TimePicker } from "material-ui-time-picker"; import { Input as Ti ...

The Ajax page does not respond to click events when the function is declared within $(function(){ }) block

Create two functions as shown below: <script> $(function () { function myFunctionB() { alert("ddd"); } }) function myFunctionA() { alert("ddd"); } </sc ...

Tips for preventing circular dependencies in JavaScript/TypeScript

How can one effectively avoid circular dependencies? This issue has been encountered in JavaScript, but it can also arise in other programming languages. For instance, there is a module called translationService.ts where upon changing the locale, settings ...

Ways to implement a fixed navigation bar beneath the primary navbar using ReactJS

Utilizing ReactJS, I am endeavoring to create a secondary (smaller) navbar in the same style as Airtable's product page. My primary navbar is situated at the top and transitions from transparent to dark when scrolled. The secondary bar (highlighted in ...

Switching the phone formatting from JavaScript to TypeScript

Below is the JavaScript code that I am attempting to convert to TypeScript: /** * @param {string} value The value to be formatted into a phone number * @returns {string} */ export const formatPhoneString = (value) => { const areaCode = value.substr(0 ...

When running JavaScript from a .js.erb file, it gets executed while still being visible as plain text

I'm facing an issue with a form inside a popup window. Here's how it looks: <%= form_tag "/controller/action", :method => :get, :remote => true do %> ... <% end %> Within my controller: respond_to do |format| format.js end ...

Converting PHP variables to JavaScript using AJAX and XML communication

In order to gain a deeper understanding, I am determined to tackle this task without relying on jQuery. This means I am willing to reinvent the wheel in order to fully comprehend how it functions. My research has led me to believe that AJAX is the key to a ...

"Encountering difficulty in retrieving information from $q and integrating it into the

I am facing an issue with binding data from an API to my scope using promises in AngularJS. Despite successfully retrieving the JSON data from the server, the $scope variable remains empty. Any assistance on this matter would be greatly appreciated. Thank ...

Having trouble downloading the compression webpack plugin using npm, as it keeps throwing an error

Hey there, this message pops up when attempting to execute the following command: npm install compression-webpack-plugin Here is the accompanying error: `PS D:\phaser games\game-slot-machine> npm install compression-webpack-plugin npm ERR! ...

How to securely upload and generate a permanent link for the contents of a zip file using express js

I am new to Javascript and Node JS. I have a challenge of uploading a zip file containing only pictures and creating permanent links for these pictures. Currently, I can upload a zip file and extract its contents using the following code snippet: var expr ...