流星订阅先获取空数据然后获取真实数据 [英] Meteor subscribe first fetch empty data then real data

查看:50
本文介绍了流星订阅先获取空数据然后获取真实数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器端发布数据,但是我订阅数据的时候,我先是空的,然后是真实的数据.一开始如何订阅真实数据?

I published data on the server side, but when I subscribe the data, I first got empty and then real data. How can I subscribe the real data at first?

class BlogItem extends Component{
  render(){
      console.log(this.props.posts);
      return(
        this.props.posts.map(post =>{
          return(
            <li className="list-group-item" key={post._id}>
              title:{post.title}
            </li>
          );
        })
      );
  };
}


export default createContainer((props) => {
  Meteor.subscribe('posts');
  return { posts: Posts.find({}).fetch() };
}, BlogItem);

在服务器上发布:

Meteor.startup(() => {
  Meteor.publish('posts', function() {
   return Posts.find({});
 });
});

推荐答案

没有办法让数据更快地到达客户端.这就是订阅的工作方式.要处理它,请使用订阅句柄来测试它是否准备好并同时显示一个加载程序.

There is no way to make the data get to the client faster. That is how a subscription works. To deal with it, use the subscription handle to test if it is ready and show a loader in the meantime.

class BlogItem extends Component {
  render() {
    const {ready, posts} = this.props;
    if (!ready) return (<div>loading...</div>);
    return (
      <ul>
        posts.map(post => (
          <li className="list-group-item" key={post._id}>
            title:{post.title}
          </li>
        ));
      </ul>
    );
  }
}

export default createContainer(() => {
  const handle = Meteor.subscribe('posts');
  return {
      ready: handle.ready(),
      posts: Posts.find({}).fetch(),
  };
}, BlogItem);

这篇关于流星订阅先获取空数据然后获取真实数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆