简体   繁体   English

在Meteor React应用中使用Mongo集合时,返回一个空数组[]

[英]When using Mongo collections in Meteor React app, returning an empty array []

When using Mongo collections in Meteor React app, returning an empty array []. 在Meteor React应用中使用Mongo集合时,返回一个空数组[]。 I was using the "react-meteor-template" from Github. 我正在使用Github的“反应流星模板”

I declared 我宣布

Posts = new Mongo.Collection("posts"); 帖子=新的Mongo.Collection(“ posts”);

in a Collection.js file. 在Collection.js文件中。

The following is where I get the empty array [] data 以下是我获取空数组[]数据的地方

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

While we tend to use subscriptions from routes/templates with blaze/iron:router, we usually subscribe from components in Blaze. 虽然我们倾向于在blaze / iron:router中使用来自路由/模板的订阅,但通常我们从Blaze中的组件进行订阅。

Here is for example a piece of code we use in one of our projects: 例如,这是我们在一个项目中使用的一段代码:

getMeteorData() {
        let subscription = Meteor.subscribe('usersList', this.state.limit);
        return {
            subscription: subscription.ready(),
            users: Meteor.users.find({}, {sort: {createdAt: -1}}).fetch()
        };
}

You can now check the subscription readyness in render() and data should normally be loading up. 现在,您可以在render()检查订阅准备情况,并且通常应该加载数据。 Note that you can still subscribe from flow-router if you want to. 请注意,您仍然可以从流路由器订阅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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