Meteor 1.3 + React:检测订阅失败?

时间:2016-04-15 12:52:02

标签: meteor meteor-publications meteor-react

我有一个简单的Meteor订阅,我在加载数据时显示加载消息。但是,如果订阅失败,我不知道如何显示错误消息。

export const MyAwesomeComponent = createContainer(() => {
  let sub = Meteor.subscribe('some-data');
  if (!sub.ready()) return { message: 'Loading...'};
  if (sub.failed()) return { message: 'Failed.' }; // How to do this?
  return {
    data: Data.find().fetch()
  }
}, MyInternalRenderComponent);

问题是,订阅对象没有failed()方法,只有ready()个查询。如何在createContainer()方法中将订阅失败作为道具传递?

我知道Meteor.subscribe方法对此案例有onStop回调,但我不知道如何将其粘贴到传递属性的位置。

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我设法让这项工作得以实现,我认为它可以回答你的问题。

请记住我正在使用Meteor 1.6,但它应该为您提供信息以使其在您身边工作。

在出版物/出版物上:

  try {
    // get the data and add it to the publication
    ...
    self.ready();
  } catch (exception) {
    logger.error(exception);
    // send the exception to the client through the publication
    this.error(new Meteor.Error('500', 'Error getting data from API', exception));
  }

在UI组件上:

const errorFromApi = new ReactiveVar();

export default withTracker(({ match }) => {
  const companyId = match.params._id;
  let subscription;

  if (!errorFromApi.get()) {
    subscription = Meteor.subscribe('company.view', companyId, {
      onStop: function (e) {
        errorFromApi.set(e);
      }
    });
  } else {
    subscription = {
      ready: () => {
        return false;
      }
    };
  }

  return {
    loading: !subscription.ready(),
    company: Companies.findOne(companyId),
    error: errorFromApi.get()
  };
})(CompanyView);

从这里你需要做的就是获得错误道具并根据需要渲染组件。

这是error道具的结构(在onStop的{​​{1}}回调中收到):

subscribe

<强> [编辑]

围绕{ error: String, reason: String, details: String } 存在条件的原因是为了避免从自然Meteor.subscribe()更新中获得的烦人的无限循环,这将导致新的订阅/来自发布的新错误等等

相关问题