Hi,
I’m new to meteor and used PHP for the majority of my time in programming.
I’ve created a --full meteor project, trying to build a simple news-feed feature, where I can write a post, with a title and display it. My form doesn’t seem to insert the data nor is the code working that displays the data of the news-collection. (I inserted a set of data via meteor-mongo). There’s no error showing up.
This is what I have:
imports/api/news/server/publications.js
// All news-related publications
import { Meteor } from 'meteor/meteor';
import { News } from '../news.js';
Meteor.publish('news.all', function () {
return News.find();
});
imports/api/news/methods.js
// Methods related to news
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { News } from './news.js';
Meteor.methods({
'news.insert'(content, title) {
check(content, String);
check(title, String);
return News.insert({
title,
content,
createdAt: new Date(),
});
},
});
imports/api/news/news.js
// Definition of the news collection
import { Mongo } from 'meteor/mongo';
export const News = new Mongo.Collection('news');
imports/ui/components/apc_news/apc_news.html
<template name="apc_news">
<section class="news_wrapper">
<article>
<h3>Write Newsfeed</h3>
<form class='news-link-add'>
<input type="text" name="title" placeholder="Title" required>
<textarea name="content" required>Write your news!</textarea>
<br>
<input type="submit" name="submit" value="Add new news">
</form>
</article>
<article>
<h3>Latest News</h3>
<ul>
{{#each news}}
{{content}}
{{title}}
{{/each}}
</ul>
</article>
</section>
</template>
imports/ui/components/apc_news/apc_news.js
import { News } from '/imports/api/news/news.js';
import { Meteor } from 'meteor/meteor';
import './apc_news.html';
Template.apc_news.onCreated(function () {
Meteor.subscribe('news.all');
});
Template.apc_news.helpers({
news() {
return News.find({});
},
});
Template.apc_news.events({
'submit .news-link-add'(event) {
event.preventDefault();
const target = event.target;
const title = target.title;
const content = target.url;
Meteor.call('news.insert', title.value, content.value, (error) => {
if (error) {
alert(error.error);
} else {
title.value = '';
content.value = '';
}
});
},
});
imports/pages/apc/apc.html
<template name="App_apc">
{{> apc_news}}
</template>
imports/pages/apc/apc.js
import './apc.html';
import '../../components/apc_news/apc_news.js';