Pub/Sub for meteor.users in React [Solved]

Hello,

Can someone look at below and let me know what I am doing wrong. Its driving me nuts…

/* imports/api/globaladmin/users.js */

export const Users = Meteor.users; //Pretty sure this is the problem!!

and…

/* imports/api/globaladmin/server/publications.js */

import { Users } from '../users';

Meteor.publish('users-list', () => Users.find()); 

The above is where I think the error is, but not sure what to do… How do you publish and subscribe Meteor.users properly in react?

My components, users-list and user are below…

/* imports/ui/components/globaladmin/users-list.js */

import React, { Component, PropTypes } from 'react';
import { createContainer } from 'meteor/react-meteor-data';

import { Users } from '../../../api/globaladmin/users.js';

import User from './user.js';

class UsersList extends Component {
  getUsers() {
    // console.log('this.props.users:', this.props.users);
    return this.props.users.map((user) => {
      console.log('user:', user);
      return (
        <User
          key = {user._id}
          user = {user}
        />
      );
    });
  }

  render() {
    console.log('this.props.users:', this.props.users);
    return (
      <div className="container">
        {this.getUsers()}
      </div>
    );
  }
}

UsersList.propTypes = {
  users: PropTypes.array.isRequired,
};

export default createContainer(() => {
  Meteor.subscribe('users-list');

  return {
    users: Users.find({}).fetch(),
  };
}, UsersList);

/* imports/ui/components/globaladmin/user.js */

import React from 'react';

export default class User extends React.Component {
  render() {
    return (
      <li>{this.props.user._id}</li>
    );
  }
}

User.propTypes = {
  user: React.PropTypes.object.isRequired,
};

Just moving to React, so making some noob mistake. The output is only the userid of the user i am logged in as, when what i am looking for is all the users. Clearly I am not subscribing properly, but not sure how to fix.

If I remove the publish and subscribe, it still works fine, which means I am not accessing the thing properly. I have turned off insecure and autopublish.

Thanks so much.

Tat

You don’t have to define the users collection. It’s already added for you in the accounts package. So you just need to do Meteor.users.find().

I need all the users, not just the current user. Do i not need to pub and sub for that?

I’ll try again, but even when i did not have the pub and sub, it was only giving me the logged in user.

Tat

Yeah you need the publication. You need to change it to Meteor.users.find(). It will publish all your users then. You don’t need the users.js and you don’t need to import users in the publication just import meteor in there.

We have literally exactly the same problem: my forum post. I don’t gather that it’s React specific, but I wonder whether it is specific to 1.3 (assuming we’re both using it). I am also (?) using this convoluted recommended file structure (which I don’t mind per se) but it straight up is only returning 1 user document of the user that is logged in. It’s pretty infuriating tbh.

1 Like

Hey @rj2249, @mistaken3lf

I worked it out. I’m a noob. I had forgotten the whole importing modules thing. So, I forgot that i had to explicitly load in the publications.js into the startup.

The code was fine, I was just a muppet. Thanks so much.

Tat