Meteor 1.3 + React: detect subscription failure?

I have a simple Meteor subscription and I am showing a download message while the data is loading. But I do not know how to display an error message if the subscription failed.

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

The problem is that the subscription object does not have a failed() method, but only a ready() request. How to pass a unsubscribe as a requisite in the createContainer() method?

I know that the Meteor.subscribe method has an onStop for this case, but I don't know how to stick it to pass a property.

+5
source share
1 answer

After much research, I managed to get this job, and I think that it answers your question.

Remember that I am using Meteor 1.6, but it should give you information to make it work on your side.

In publication / publication:

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

In the user interface component:

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

From here you only need to get the support of the error and display the component as desired.

This is the error prop structure (obtained by the onStop from subscribe ):

 { error: String, reason: String, details: String } 

[change]

The reason the Meteor.subscribe() condition Meteor.subscribe() is to avoid the annoying endless loop that you will get from the natural updates withTracker() , which will cause new publications / new errors from the publication, etc.

0
source

Source: https://habr.com/ru/post/1247213/


All Articles