[Solved] Updating db post - Error 500

#1

Hi,

My intention is to build a newsfeed-feature where I can insert, edit and delete content. I’m stuck at the edit part.

imports/api/news/methods.js

Meteor.methods({

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

    // Make sure the user is logged in before insertig 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);
  },

// Doesn't work?
'news.update'(content, title, newsId) {
  check(content, String);
  check(title, String);
  check(newsId, String);

  // user has to be logged in before updating content
  if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}

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

I inserted a route that leads to the update-form for the content:

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

The update-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>

imports/ui/components/news_edit/news_edit.js

And the js-file which is calling the method:

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;
    const title = target.elements.title;
    const content = target.elements.content;

    Meteor.call('news.update', title.value, content.value, (error) => {
      if (error) {
        console.log("code fails here");
        alert(error.error);
      }  
    });
  },
});

If I click on submit it throws the error where I wrote the console.log.

Any help on how to solve this would be appreciated!

#2

You need to share the server error log. But it seems you’ve a typo in your NewsId variable, the N should be lower case, so most likely you’re getting undefined error at the server.

1 Like
#3

Thank you for replying. I changed it to lower case. But I still get the 400 Error.

Error log from the console:

I20190221-18:26:31.640(1)? Exception while invoking method 'news.update' { Error: Match error: Expected string, got undefined
I20190221-18:26:31.642(1)?     at check (packages/check/match.js:36:17)
I20190221-18:26:31.643(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:35:3)
I20190221-18:26:31.644(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190221-18:26:31.644(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190221-18:26:31.645(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.646(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190221-18:26:31.647(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.647(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190221-18:26:31.648(1)?     at new Promise (<anonymous>)
I20190221-18:26:31.649(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190221-18:26:31.652(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190221-18:26:31.653(1)?   message: 'Match error: Expected string, got undefined',
I20190221-18:26:31.653(1)?   path: '',
I20190221-18:26:31.654(1)?   sanitizedError:
I20190221-18:26:31.654(1)?    { Error: Match failed [400]
I20190221-18:26:31.655(1)?     at errorClass.<anonymous> (packages/check/match.js:91:27)
I20190221-18:26:31.656(1)?     at new errorClass (packages\meteor.js:725:17)
I20190221-18:26:31.657(1)?     at check (packages/check/match.js:36:17)
I20190221-18:26:31.657(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:35:3)
I20190221-18:26:31.658(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190221-18:26:31.658(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190221-18:26:31.659(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.660(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190221-18:26:31.662(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.663(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190221-18:26:31.664(1)?     at new Promise (<anonymous>)
I20190221-18:26:31.664(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190221-18:26:31.665(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190221-18:26:31.666(1)?      isClientSafe: true,
I20190221-18:26:31.666(1)?      error: 400,
I20190221-18:26:31.668(1)?      reason: 'Match failed',
I20190221-18:26:31.669(1)?      details: undefined,
I20190221-18:26:31.670(1)?      message: 'Match failed [400]',
I20190221-18:26:31.670(1)?      errorType: 'Meteor.Error' },
I20190221-18:26:31.673(1)?   errorType: 'Match.Error' }
I20190221-18:26:31.674(1)? Sanitized and reported to the client as: { Error: Match failed [400]
I20190221-18:26:31.674(1)?     at errorClass.<anonymous> (packages/check/match.js:91:27)
I20190221-18:26:31.675(1)?     at new errorClass (packages\meteor.js:725:17)
I20190221-18:26:31.676(1)?     at check (packages/check/match.js:36:17)
I20190221-18:26:31.677(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:35:3)
I20190221-18:26:31.677(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190221-18:26:31.678(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190221-18:26:31.679(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.681(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190221-18:26:31.685(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190221-18:26:31.685(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190221-18:26:31.686(1)?     at new Promise (<anonymous>)
I20190221-18:26:31.687(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190221-18:26:31.687(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190221-18:26:31.688(1)?   isClientSafe: true,
I20190221-18:26:31.689(1)?   error: 400,
I20190221-18:26:31.689(1)?   reason: 'Match failed',
I20190221-18:26:31.690(1)?   details: undefined,
I20190221-18:26:31.690(1)?   message: 'Match failed [400]',
I20190221-18:26:31.691(1)?   errorType: 'Meteor.Error' }
#4

It looks like you forgot to pass the newsId in your Meteor.call()

2 Likes
#5

Could you maybe explain how I pass the newsId?

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

    const target = event.target;
    const title = target.elements.title;
    const content = target.elements.content;

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

This is my guess but it doesn’t work and I’m not sure where to get it from. Do I have to get the newsId from the form?

#6

The error message is pretty telling

I20190221-18:26:31.640(1)? Exception while invoking method 'news.update' { Error: Match error: Expected string, got undefined

it reports that one of the expected (checked) parameters supposed to be a String but it got undefined.

The method signature for the parameters is (content, title, newsId) but when you call the method from your event-handler you commit the last parameter Meteor.call('news.update', title.value, content.value, ... the the error you are seeing is entirely to be expected, just as @dnszero already pointed out

Your attempt to pass the newsId conflicts with how you defined your method. You should try

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

Thank you, I tried this. This error showed up:

news_edit.js:22 Uncaught ReferenceError: newsId is not defined
    at Object.submit .news-link-update (news_edit.js:22)
    at blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3818
    at Function.Template._withTemplateInstanceFunc (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3769)
    at Blaze.View.<anonymous> (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3817)
    at blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:2617
    at Object.Blaze._withCurrentView (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:2271)
    at Blaze._DOMRange.<anonymous> (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:2616)
    at HTMLFormElement.<anonymous> (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:863)
    at HTMLDivElement.dispatch (modules.js?hash=982e98e4158b22d0eb93d0ec0cd347e5e87b2c44:30781)
    at HTMLDivElement.elemData.handle (modules.js?hash=982e98e4158b22d0eb93d0ec0cd347e5e87b2c44:30589)

newsId is not defined: Do I have to save this._id in a variable in order to define it?

#8

Again, the error is correct. You are accessing a variable newsId that is not defined in your code. You can embed the id in the form like so

<form data-news-id="{{news._id}}">
</form>

and access it in your event handler as

let newsId = target.dataset.newsId;
1 Like
#9

You can get the id the exact same way that you are already getting the id earlier in the file:

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

Specifically:

    var id = FlowRouter.getParam('id');
1 Like
#10

I’m still struggling.

<template name="NewsEdit">
<div class="col">
    <small>Update: <b>{{news.content}}</b></small>
    <form class='news-link-update' data-news-id="{{news._id}}">
    <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>
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(error.reason);
        alert(error.reason);
      }  
  });
  },
});

The Matching Error, 400, is gone now. Now I have a 500 error, internal server error. Did I do something wrong? I’m starting to think that my whole approach isn’t good.

Error-log:

I20190222-04:22:58.230(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:22:58.233(1)?     at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:22:58.233(1)?     at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:22:58.234(1)?     at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:22:58.234(1)?     at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:22:58.235(1)?     at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:22:58.236(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:22:58.236(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:22:58.237(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:22:58.237(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:22:58.238(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:22:58.239(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:22:58.239(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:22:58.242(1)?     at new Promise (<anonymous>)
I20190222-04:22:58.242(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:22:58.243(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190222-04:23:28.905(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:23:28.906(1)?     at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:23:28.910(1)?     at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:23:28.911(1)?     at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:23:28.911(1)?     at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:23:28.912(1)?     at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:23:28.912(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:23:28.913(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:23:28.914(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:23:28.915(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:23:28.916(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:23:28.918(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:23:28.918(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:23:28.919(1)?     at new Promise (<anonymous>)
I20190222-04:23:28.920(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:23:28.920(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190222-04:25:34.580(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:25:34.583(1)?     at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:25:34.584(1)?     at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:25:34.585(1)?     at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:25:34.585(1)?     at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:25:34.586(1)?     at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:25:34.587(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:25:34.587(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:25:34.588(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:25:34.593(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:25:34.594(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:25:34.594(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:25:34.594(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:25:34.595(1)?     at new Promise (<anonymous>)
I20190222-04:25:34.596(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:25:34.597(1)?     at packages/ddp-server/livedata_server.js:559:43
I20190222-04:27:21.274(1)? Exception while invoking method 'news.update' Error: Invalid modifier. Modifier must be an object.
I20190222-04:27:21.277(1)?     at MongoConnection._update (packages/mongo/mongo_driver.js:495:11)
I20190222-04:27:21.278(1)?     at MongoConnection.<anonymous> (packages\meteor.js:376:21)
I20190222-04:27:21.279(1)?     at MongoConnection.(anonymous function) [as update] (packages/mongo/mongo_driver.js:754:49)
I20190222-04:27:21.280(1)?     at _0xb3b0x8.(anonymous function).update (packages/mongo/collection.js:585:31)
I20190222-04:27:21.280(1)?     at _0xb3b0x8.(anonymous function).Mongo.Collection.(anonymous function) [as update] (packages/aldeed:collection2/collection2.js:213:19)
I20190222-04:27:21.281(1)?     at MethodInvocation.news.update (imports/api/news/methods.js:40:10)
I20190222-04:27:21.282(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20190222-04:27:21.283(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20190222-04:27:21.283(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:27:21.286(1)?     at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20190222-04:27:21.286(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1304:12)
I20190222-04:27:21.287(1)?     at Promise (packages/ddp-server/livedata_server.js:715:46)
I20190222-04:27:21.288(1)?     at new Promise (<anonymous>)
I20190222-04:27:21.289(1)?     at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20190222-04:27:21.289(1)?     at packages/ddp-server/livedata_server.js:559:43
#11

A syntax error in my methods caused the 500 error.

Thanks to Sudheer Jami from StackOverflow:
https://stackoverflow.com/questions/54817702/updating-a-post-in-meteor-js/54820034#54820034

    Meteor.methods({
      ...
      // Doesn't work?
      news.update(content, title, newsId) {
          check(content, String);
          check(title, String);
          check(newsId, String);

          // user has to be logged in before updating content
          if (! Meteor.userId()) {throw new Meteor.Error('not-authorized');}

          News.update({_id: newsId}, {  //The parentheses was closed before the $set in your code
             $set: {
               title:title, 
               content:content
             },
             {
               multi:false // optional since by default it is false
             }
          });
      }

});