流星:无法从数据库插入和拉出

时间:2015-12-18 16:39:12

标签: javascript mongodb meteor

我正在关注流星教程(在这里和那里做一些小改动),当涉及到插入和拔出数据库时,我似乎没有太多运气。

这是我的html文件:

<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>

和我的javascript文件:

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
        });
    }
})

我没有对数据库进行任何更改(使用预装了meteor的那个)

问题是当我填写表单并提交时,没有任何内容存储在集合中。我通过查看数据库本身验证了这一点,没有任何内容。

此外,我使用mongo shell(db.jobs.insert({ name: test, createdAt: new Date()});)手动将文档插入到集合中,并且可以验证文档是否存在于集合中。但是,它不会出现在无序列表中的网站上。

我删除了insecureautopublish包,如果这有所不同

我的代码阻止客户端/服务器与数据库通信有什么问题?

1 个答案:

答案 0 :(得分:0)

我最终重新创建了项目,从那时起一切都有效。

使用新项目,我将所有文件放在根目录中,这似乎可以解决问题,而在我将事物拆分为/ client和/ server目录之前