简体   繁体   中英

meteor insert item into collection not functioning

I have created a collection with two items in place (entered using the command console) and am now developing a way to enter new items into the collection from the app.

When I type a new name and hit [enter] the log shows that the form has submitted and that the event has taken the input on board, but the actual collection remains untouched at just two items.

Given that the log is showing that the event reads the form input properly, I suspect that the problem is within the Blogs.insert bit, but I'm just not sure what's wrong.

HTML:

<form class="newblog">
    <input type="text" name="blogname" placeholder="New Blog Name"/>
</form>

JS:

Blogs = new Mongo.Collection("blogs");

//...irrelevant stuff for the rest of the app...

Template.blogroll.events({
    "submit .newblog": function (event) {
        // prevent browser default behaviour
        event.preventDefault();

        // log input
        console.log(event);

        // get value from form element
        var blog_to_enter = event.target.blogname.value;

        // insert a blog into the collection
        Blogs.insert({
            blog: blog_to_enter,
            created: new Date()
        });

        // clear form
        event.target.blogname.value = "";
    }
});

See this meteorpad for a example. Maybe you have defined some allow/deny rules to deny the insertion of blogs or you just forgot to subscribe to the Blog collection?

Don't define the var blog_to_enter = event.target.blogname.value; Instead of:

Blogs.insert({
            blog: blog_to_enter,
            created: new Date()
        });

Use "blogname":

Blogs.insert({
            blog: blogname,
            created: new Date()
        });

It should work that way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM