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!