Meteor + React - 在访问文档时加载 vs 404

Meteor + React - Loading vs 404 while accessing a document

我有以下代码:

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

我还有 autopublish 包,这个组件正在以下(动态)路由中呈现(我正在使用 react-router 进行路由)

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

只要在集合中找到具有此 ID 的文档,一切正常。 如果在集合中找到文档,则显示名称属性,否则显示 'Loading'。 这才是真正的问题所在。如果作为参数传递的 'id' 在集合中没有对应的文档怎么办? 它永远显示 'Loading'。

我怎样才能改为显示 404?

即使使用自动发布,如果您的文档不存在(因为尚未加载或不在集合中),您在没有订阅的情况下也无法确定。

基本上 autopublish 为每个集合创建一个默认发布给所有客户,returns 所有文档立即。

但是,拥有大量文档可能会导致您不知道 Id 的文档是否尚未加载的状态。

作为解决方法,您可以创建一个返回所有文档的简单发布并订阅它。