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

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

本文关键字:一个 数组 返回 应用程序 React Meteor 集合 Mongo      更新时间:2023-09-26

在 Meteor React 应用程序中使用 Mongo 集合时,返回一个空数组 []。我使用的是Github的"react-meteor-template"。

我宣布

帖子 = 新的 Mongo.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>
       )
    }
});

虽然我们倾向于使用带有blaze/iron:router的路由/模板订阅,但我们通常从Blaze中的组件订阅。

例如,这是我们在一个项目中使用的一段代码:

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

现在可以在render()中检查订阅就绪情况,并且数据通常应该正在加载。请注意,如果您愿意,您仍然可以从流路由器订阅。