Elia Weiss
Elia Weiss

Reputation: 9915

Meteor/React - display all users (publish all users to the client )

I'm using Meteor/React and I'm trying to display all the users in the system:

I have: /imports/api/users.js

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';     

if (Meteor.isServer) {
  // This code only runs on the server
  // Only publish tasks that are public or belong to the current user
  Meteor.publish('allUsers', function () {
    return Meteor.users.find({}, {fields: {"emails.address": 1}});  
  });    
}

and /imports/ui/App.jsx

import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
class App extends Component {

    constructor() {
        super();
        this.state = {
            subscription: {
                users: Meteor.subscribe('allUsers')
            }
        }
    }

    componentWillUnmount() {
        this.state.subscription.users.stop();
    }


    render() {
        let users = this.props.users;
        console.log(users);
        return (<div>
            <h1>GoArc User Manager</h1>

            <div>
                {users.map((user)=>{
                    if ('emails' in user ) {
                        email = user.emails[0].address;
                    } else {
                        email = '?'
                    }
                    return <div key={user._id}>{user._id} - {email}</div>
                })
                }
            </div>
        </div>)
    }
}


export default createContainer(() => {
  return {
    users: Meteor.users.find({ }).fetch(),
  };
}, App);

But it still show only the current login user.

If I set autopublish/insecure on then the code is working correct.

What is the correct way to publish all users to the client with Meteor/React?

Another related issue is that even when I set autopublish/insecure on still the email.address field appear only for the current user - even though I published this field:

return Meteor.users.find({}, {fields: {"emails.address": 1}}); 

Upvotes: 1

Views: 835

Answers (1)

Elia Weiss
Elia Weiss

Reputation: 9915

The code is correct but I forgot to add import '../imports/api/users.js'; in /server/main.js

Upvotes: 1

Related Questions