Cant insert and pull from database

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?

It appears that whenever I try to add something using the form, I get this error in my browser:

Error invoking Method 'addJob': Method not found [404]

Which is strange to me because it’s explicitly in my the meteor methods section in the js file

I’ve also tried hoisting the meteor methods section to above the client/server logic but that didn’t seem to make a difference

I just threw your code into a quick Meteor Pad (http://meteorpad.com/pad/uMwpcqtF6EHfcDs4d/forums-15102 if you want to see it), and it appears to be working properly. Must be something else on your side.

You define your Methods to live on both server and client, but Meteor.userId() does not exist on the server, so thats probably where your code fails.

if(Meteor.isServer)
Meteor.methods({
    addJob: function(name){
        if (! this.userId) {
            throw new Meteor.Error("not-authorized");
        }

        Jobs.insert({
            name: name,
            createdAt: new Date(),
            owner: this.userId,
            username: Meteor.users.findOne(this.userId).username
        });
    }
})

I just created a new project from scratch and now everything seems to work.

It appears that its due to the directory structure I was using before, where everything was inside a //server directory. after I removed everything to the project root it started working

What is the correct way to structure a meteor project? Surely not everything just gets dumped into the root directory?

Well, the official MDG recommended application structure that will be included in the new Meteor Guide, is in flux - see https://github.com/meteor/guide/blob/master/outlines/structure.md. That being said take a look at the Structuring Your Application section of the docs, then start searching through these forums for application structure recommendations / examples. You’ll find a lot of them, as well as links to several boilerplate starter projects with defined application structures in GitHub (like https://github.com/SachaG/Void, https://github.com/Differential/meteor-boilerplate, https://github.com/matteodem/meteor-boilerplate, etc.).