空对象的回退消息 meteor/react

Fallback message for empty object meteor/react

如果没有要返回的对象,我想显示一条消息。类似于:'currently there are no customers available'。 我尝试对 Object.getOwnPropertyNames() 进行一些修改,但无法使其正常工作,因为未调用映射函数。我不确定将此检查放在哪里,在渲染函数中,在模板中的跟踪器或渲染调用中。

我使用 Meteor/react,我的代码如下所示:

import React, {Component} from 'react';
import {withTracker} from 'meteor/react-meteor-data';
import {Link} from 'react-router-dom';

class ArchiveCustomerOverview extends Component {
  renderCustomerList() {
    return this.props.users.map( user => {
        return(
            <div className="row" key={user._id}>
                <Link to={}>
                    <span className="medium-1">{user.profile.name}</span>
                    <span className="medium-4">{user.profile.company}</span>
                    <span className="medium-3">{user.profile.phone}</span>
                    <span className="medium-3">{user.emails[0].address}</span>
                </Link>
            </div>
        )
    });
}
render() {
    return (
        <div>
            <div className="list-overview">
                <div className="list-wrapper">
                    <div className="list-box clear">
                        {this.renderCustomerList()}
                    </div>
                </div>
            </div>
        </div>
    );
  }
}
export default withTracker( (props) => {
  Meteor.subscribe('users');
   return { 
    users: Meteor.users.find({
        'profile.isArchived': 0,
        'roles.customers'       : 'customer'
    }).fetch() 
  };
})(ArchiveCustomerOverview);

在像这样渲染之前先检查用户数量:

  renderCustomerList() {
    if (this.props.users.length === 0) {
      return (<div>Currently there are no customers available</div>)
    }
    return this.props.users.map( user => {

但请注意:您可能无法从用户集合中得到您想要的东西 - 出于安全原因,它与其他集合的处理方式不同。