Meteor 删除了自动发布和不安全:现在我可以';t通过MongoDB检索数据
我开始学习Meteor.js,当我删除了autopublish和Unsecure后,我的应用程序就不再工作了。 我可以将数据插入数据库,但不能按数据库检索任何内容。 控制台中没有错误。 这是我的代码: client/main.htmlMeteor 删除了自动发布和不安全:现在我可以';t通过MongoDB检索数据,meteor,Meteor,我开始学习Meteor.js,当我删除了autopublish和Unsecure后,我的应用程序就不再工作了。 我可以将数据插入数据库,但不能按数据库检索任何内容。 控制台中没有错误。 这是我的代码: client/main.html <head> <title>Blog Test</title> </head> <body> <h1>Blog Test</h1> {{>blog}
<head>
<title>Blog Test</title>
</head>
<body>
<h1>Blog Test</h1>
{{>blog}}
{{>ListBlogs}}
</body>
<template name="blog">
<form class="blog-post" id="blog-post" role="form">
<label id="label-title" class="label-title">Titolo:</label>
<input id="input-title" class="input-title" placeholder="Titolo">
<br>
<label id="label-text" class="label-text">Testo:</label>
<textarea id="input-text" class="input-text" placeholder="Testo"></textarea>
<br>
<button type="submit" class="blog-btm">Submit</button>
</form>
</template>
<template name="ListBlogs">
{{#each posts}}
<h2>{{title}}</h2>
<p>{{text}}</p>
{{/each}}
</template>
server/server.js
Meteor.methods({
'submitPost':function(title, text){
console.log("Titolo: " + title);
console.log("Testo:" + text);
Blogs.insert({title:title, text:text});
}
});
Meteor.publish('posts', function() {
return Blogs.find();
});
lib/blog.js
Blogs = new Meteor.Collection('blogs');
我们在IRC中解决了这个问题,我相信:-) 问题是你的模板
ListBlogs
不知道posts
的意思。您必须定义一个helper,以便{{{each posts}}
块具有意义
幸运的是,这只需要非常快速的修复:
Template.ListBlogs.posts = function(){
return Blogs.find().fetch();
}
把它放到你的
客户端/main.js中,所有帖子都会实时显示。你的代码没问题,不过你必须尝试用这种方式订阅收藏
Tracker.autorun(function () {
Meteor.subscribe("posts");
});
然后你必须创建助手,例如
Template.blog.helpers({
})
Template.ListBlogs.helpers({
posts: function(){
return Posts.find({});
}
})
从“posts”检索项目的代码在哪里?此处不需要.fetch()
。
Template.blog.helpers({
})
Template.ListBlogs.helpers({
posts: function(){
return Posts.find({});
}
})