gpt4 book ai didi

javascript - 空对象 meteor / react 的后备消息

转载 作者:行者123 更新时间:2023-12-03 01:02:16 25 4
gpt4 key购买 nike

如果没有要返回的对象,我想显示一条消息。比如:“目前没有可用的客户”。我尝试对 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 => {

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

关于javascript - 空对象 meteor / react 的后备消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52570347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com