I’m following along with the meteor tutorial (making a few minor changes here and there) and when it comes to inserting and pulling from the database I don’t seem to be having much luck.
Here is my html file:
<head>
<title>test server</title>
</head>
<body>
<header>
<h1>test server</h1>
<div class="loginButton">{{ >loginButtons}}</div>
</header>
{{#if currentUser}}
<nav>
<h1>Jobs</h1>
<form class="new-job">
<input type="text" name="job" placeholder="Type to enter new job">
</form>
<ul>
{{#each jobs}}
{{> job}}
{{/each}}
</ul>
</nav>
<main>
</main>
{{/if}}
</body>
<template name="job">
<li>{{name}}</li>
</template>
and my javascript file:
Jobs = new Mongo.Collection("jobs");
if (Meteor.isServer){
Meteor.publish("jobs", function(){
return Jobs.find()
})
}
if (Meteor.isClient){
Meteor.subscribe("jobs");
Template.body.helpers({
jobs: function(){
return Jobs.find({}, {sort: {createdAt: -1}});
}
});
Template.body.events({
"submit .new-job": function(event){
event.preventDefault();
var name = event.target.job.value;
Meteor.call("addJob", name);
event.target.job.value = "";
}
})
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
Meteor.methods({
addJob: function(name){
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
Jobs.insert({
name: name,
createdAt: new Date(),
owner: Meteor.userId(),
username: Meteor.user().username
});
}
})
I haven’t made any changes to the database (using the one preinstalled with meteor)
The problem is when I fill in the form and submit, nothing gets stored in the collection. I verify this by taking a look at the db itself and nothing is there.
Furthermore, I manually inserted a document into the collection using the mongo shell (db.jobs.insert({ name: test, createdAt: new Date()});
), and can verify that the document exists in the collection. However, it does not appear on the website within the unordered list.
I have removed the insecure
and autopublish
packages if that makes a difference
What is wrong with my code that is preventing the client/server from talking to the database?