Reputation: 347
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 = "";
}
});
Upvotes: 0
Views: 81
Reputation: 789
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?
Upvotes: 2
Reputation: 182
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
Upvotes: 0