Gurpreet Singh
Gurpreet Singh

Reputation: 55

Meteor + React - Loading vs 404 while accessing a document

I have the following code:

import React, { Component } from 'react';
import { withTracker } from 'react-meteor-data';
import Auctions from '../collections/Auctions';

class AuctionPage extends Component {
    render() {
        return (
            <div>
                {this.props.auction? this.props.auction.name : 'Loading'}
            </div>
        );
    }
}

export default withTracker((props) => {
    const auction = Auctions.findOne({_id: props.match.params.id});
    return {
        auction,
    };
})(AuctionPage);

I still have the autopublish package and this component is being rendered at the following (dynamic) route (I am using react-router for routing)

<Route path={'/auction/:id'} component={AuctionPage} />

Everything works fine as long as the document with this id is found in the collection. The name attribute is displayed if the document is found in the collection and 'Loading' is displayed otherwise. This is where the real problem is. What if the 'id' passed as the parameter doesn't have a corresponding document present in the collection? It displays 'Loading' forever.

How can I go about displaying a 404 instead?

Upvotes: 0

Views: 45

Answers (1)

Jankapunkt
Jankapunkt

Reputation: 8423

Even using autopublish you can't know for sure without a subscription if your doc is not present, because of being not yet loaded or because of not being in the collection.

Basically autopublish creates for each collection a default publication to all clients that returns all documents immediately.

However, having a large amount of documents could result in a state where you don't know if the doc by Id is just not yet loaded.

As workaround you can just create a simple publication returning all docs and subscribe to it.

Upvotes: 1

Related Questions