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?