Enhancing a blog entry in Meteor.js

Recently, I embarked on a journey to learn Meteor.js in hopes of creating a newsfeed feature that allows me to add, edit, and delete content. As of now, I am facing challenges with the editing process.

imports/api/news/methods.js

    Meteor.methods({

  'news.insert'(content, title) {
    check(content, String);
    check(title, String);

    // Ensure user is logged in before adding content
    if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}

    return News.insert({
      content,
      title,
      createdAt: new Date(),
    });
  },

  'news.remove'(newsId) {
    check(newsId, String);
    News.remove(newsId);
  },

'news.update'(content, title, newsId) {
  check(content, String);
  check(title, String);
  check(newsId, String);

  // User must be logged in to update content
  if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}

    News.update({_id: newsId}), {
      $set: {
        title:title, 
        content:content
      }
    }
  }

});

I added a route that leads to the edit form for the content:

<a href="/apc_news/{{_id}}">Edit</a>

The edit form: imports/ui/components/news_edit/news_edit.html

<template name="NewsEdit">
<div class="col">
    <small>Update: <b>{{news.content}}</b></small>
    <form class='news-link-update'> 
    <input type="text" name="content" value="{{news.content}}" required> <small>ID: {{news._id}}</small> <br> 
    <br>
    <textarea class="form-control" rows="5" id="comment" name="title" required>{{news.title}}</textarea> 
    <br>
    <input type="submit" name="update" value="Update News" class="submit">
    </form>
</div>
</template>

Functionality implemented in the JavaScript file calling the method: imports/ui/components/news_edit/news_edit.js

    import { News } from '/imports/api/news/news.js';
    import { Meteor } from 'meteor/meteor';
    import './news_edit.html';

    Template.NewsEdit.helpers({
      news: ()=> {
        var id = FlowRouter.getParam('id');
        return News.findOne({_id: id});
      }

    });


    Template.NewsEdit.events({
  'submit .news-link-update'(event) {
    event.preventDefault();


    const target = event.target;
    let newsId = target.dataset.newsId;
    const title = target.elements.title;
    const content = target.elements.content;

    Meteor.call('news.update', title.value, content.value, newsId, (error, result) => {
      if (error) {
        console.log(newsId);
        alert(error.reason);
      }  
  });
  },
});

Now, instead of receiving a 400 error, I'm encountering a 500 Internal Server Error. It seems the ID is correctly referenced.

I20190222-04:22:58.230(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
...
I20190222-04:27:21.289(1)?     at packages/ddp-server/livedata_server.js:559:43

^ This recent error log indicates an "invalid modifier" issue. Any insights on what this might mean?

Answer №1

The reason for the

Error: Invalid modifier. Modifier must be an object.
error is due to a syntax issue within the update statement of the server method.

imports/api/news/methods.js

  Meteor.methods({
      ...
      news.update(content, title, newsId) {
          check(content, String);
          check(title, String);
          check(newsId, String);

          // In order to update content, user must be logged in
          if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}
          // *** The closing parentheses was misplaced before '$set' ***
          News.update({_id: newsId}, {
             $set: {
               title:title, 
               content:content
             },
             {
               multi:false // This is optional as it defaults to 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

How to utilize local functions within a ko.computed expression

Why isn't this line of code working? I'm using durandal/knockout and my structure is like this define(function () { var vm = function() { compute: ko.computed(function() { return _compute(1); // encountering errors }); ...

String refs are not allowed for function components. It is advised to use useRef() instead, especially when working with Nextjs

I recently encountered an error when trying to add data to the server using Next.js with the fetch method and thunk. The error message I received was: Error: Function components cannot have string refs. We recommend using useRef() instead. addBookForm.j ...

Swapping out an entire item in a designated array index for a different object

I am faced with a JavaScript object conundrum: const myValidation = [ { id:'1', name:'qqq', field1: 'Yes', field2: 'No', field3: 'Yes' }, { id:'330', name:'www', ...

After cloning the variable from props, the Vue 3 Composition API variable becomes undefined

I have a main component containing code and tables, including a modal that is displayed based on a boolean value. The main component features the following modal: <ConfirmPaymentModal ref="confirmPaymentModal" :invoice="markAsPa ...

Ensure that variables are accessible to asynchronous calls without the use of closures

As a newcomer to the world of javascript, I've been trying to navigate the realm of nested functions. Let's explore the following two examples: // example 1 var x = 45; function apple(){ var y = 60; setTimeout(function(){ console ...

The 'log' property cannot be found on the type '{ new (): Console; prototype: Console; }' - error code TS2339

class welcome { constructor(public msg: string){} } var greeting = new welcome('hello Vishal'); Console.log(greeting.msg); I encountered an error at the Console.log statement. The details of the error can be seen in the image attached below. ...

What is the process of combining JS functions with PHP echo in code?

I am currently working on creating a popout menu for an array of values that are being displayed from the database. The goal is to show the corresponding popout menu when the svg is clicked, but I am running into an issue where it only works for the first ...

Next.js version 13 is causing the page to refresh each time the router is pushed

I am currently developing a search application using NextJs 13, and I have encountered an issue where the page refreshes every time I click the search button. Strangely, this only happens when the application is deployed on Vercel. When running the app l ...

Showing nested arrays in API data using Angular

I would like to display the data from this API { "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", & ...

Why is inner HTML returning input/textbox instead of the value?

I need help extracting the value from an input/textbox within a table cell. Although the rest of my code is functioning correctly, I'm struggling to retrieve the value from this particular input element. I've attempted to access the value using ...

Detecting a mobile device when using NextJS can be accomplished by using user

With so many topics and questions on Stack Overflow, I am struggling to find the most efficient solution for detecting mobile devices. I have two components - one designed solely for desktops and another for mobile devices. {isMobile? (<SecondComponen ...

Is there a more efficient approach to configuring properties in AngularJS while maintaining a binding?

When trying to set a property in a service equal to data returned from a network call, the conventional method goes like this: //SERVICE// thisService.property = data; //SERVICE// The corresponding controller code usually looks something like this: //CO ...

Is it possible for me to dictate the sequence in which javascript / jQuery events are triggered?

Issue at Hand In my asp.net webform, I have a grid where users can update textboxes. These updates trigger a WebMethod call, updating the rest of the row without saving. To save the changes, users must click a save button. While this system works well in ...

Searching in MongoDB using periods

Hey, I'm running into an issue with the full text search feature in MongoDB. Let me give you an example: db.test.insert({fname:"blah.dll"}) db.test.insert({fname:"something.dll"}) db.test.ensureIndex({fname:"text"}) After setting this up, if I run.. ...

Storing information in a parse table using NodeJs

I am trying to add data to a Parse table. At the top of my code, I have the line import Parse from 'parse/node';. Here is how I am inserting the data: const game = Parse.Object.extend('Game'); const query = new Parse.Query(game); que ...

Utilize data retrieved from an API to customize the appearance of buttons through the combination of React and Sass styling techniques

I retrieved data from an API and used it to create a list of buttons. The data I received includes names and color codes. { name: "bob", colorCode: "#DC7472" }, { name: "ben", colorCode: "#69DCD1" }, { name: &q ...

Error in Heroku deployment - Express and React app displaying a white screen

I am encountering a challenging situation as I attempt to understand the issue at hand. Following the deployment of my React/Express application on Heroku, the build and deployment proceed without errors, but the React frontend appears blank. The browser ...

Why is JavaScript unable to fetch JSON data from C when the uint32 value is a multiple of 256 plus 10?

After receiving some helpful feedback, I decided to make some changes to this question that I posted yesterday. The title and content have been edited for clarity based on the new information I've learned. Recently, I've been utilizing C code to ...

I am unable to make changes to the Text Field component in Material-UI

After developing a React App using Material-UI, I decided to create independent Components. Below are the independent components (<PanelDiv/>): render() { return ( <div className="panelDiv-component" style={{display:this.prop ...

Is it possible to activate every function within a prototype?

When presented with a class structure as demonstrated below, I am able to iterate through all its PropertyNames using console.log. class Security { constructor(param: ParamType) { this.method1(param); ... this.methodN(param); } method1(p ...