【发布时间】:2018-08-06 01:59:06
【问题描述】:
我想创建一个表格来显示我的所有用户。我使用 Meteor 的 account-ui 包来创建新用户。
但是,当我在 React 组件中调用用户时,它只会返回一个空对象。 有什么我忘记了吗?像进口一样吗?
imports/api/users.js 用于发布:
import { Meteor } from 'meteor/meteor';
if (Meteor.isServer) {
Meteor.publish('users', function() {
return Meteor.users.find({});
})
}
然后我调用我的组件并使用我订阅的withTracker:
import React from 'react';
import { Mongo } from 'meteor/mongo'
import { withTracker } from 'meteor/react-meteor-data';
class CollectionTable extends React.Component {
render() {
return (
<table className="table table-hover table-responsive">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
{this.props.users.map(u => {
<tr>
<th scope="row">{u._id}</th>
<td>{u.profile.firstname}</td>
<td>{u.profile.lastname}</td>
<td>{u.profile.phonenumber}</td>
</tr>
})}
</tbody>
</table>
);
}
}
export default withTracker(() => {
Meteor.subscribe('users');
return {
users: Meteor.users.find().fetch()
};
})(CollectionTable);
【问题讨论】:
-
您是否在
server/main.js中导入了users.js文件? -
嗨@Jankapunkt 谢谢你的帮助。我没有导入它。我刚刚在
server/main.js中添加了import '../imports/api/users.js';,但并没有解决问题