【发布时间】:2016-05-27 07:54:01
【问题描述】:
在 Meteor React 应用程序中使用 Mongo 集合时,返回一个空数组 []。 我使用的是 Github 的 "react-meteor-template"。
我声明了
Posts = new Mongo.Collection("posts");
在 Collection.js 文件中。
下面是我得到空数组[]数据的地方
ReadPost = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
return {
postsLoading: Posts.find().fetch()
}
},
render() {
let { postsLoading } = this.data;
console.log(postsLoading);
return (
<div className="container">
{
postsLoading.map((post) => {
return (
<div key={post._id} className="col-sm-6 col-sm-offset-3" style={{'marginBottom':"30px", padding: "20px", background: "#FFBABA"}}>
<p>Reading post {this.props.postName}</p>
<h1 style={{display: "inline"}}>{post.title}</h1>
</div>
)
})
}
</div>
)
}
});
【问题讨论】:
-
我不知道 React 是如何工作的,但是在 Meteor 中你应该检查你的订阅是否设置正确。
-
我必须使用订阅吗?
-
如果您使用自动发布,那么不,您不必订阅。但即便如此,服务器响应可能需要时间才能到达客户端:这可能解释了空数组。加载页面后,从控制台运行
Posts.find().fetch(),查看数据是否实际加载到 minimongo 数据库中。
标签: javascript mongodb meteor reactjs meteor-react