Passing data to function with createContainer

Hi community,

I would like passing my data for test if user is logged and if is admin for render my component with createContainer.

My AdminLayout (using in my React Router v4) :

const AdminLayout = ({component: Component, ...rest}) => {

  console.log(AdminContainer)

  if (AdminLayout === true) {

    return (
      <Route {...rest} render={matchProps => (
        <div className="app-container">
          <HeaderAdmin />
            <main className="l-main">
              <Component {...matchProps} />
            </main>
          <FooterAdmin />
        </div>
      )} />
    )

  } else {
    return (
      <Redirect push to="/connexion"/>
    )
  }
};

AdminLayout.propTypes = {
  isLogged: React.PropTypes.bool,
  isAdmin: React.PropTypes.bool
}

AdminContainer = createContainer(() => {
  const isLogged = Meteor.userId();
  const isAdmin = Meteor.call('is-admin', Meteor.userId(), function(err, data) {
    if (err) {
      return console.log(err);
    } else {
      return data;
    }
  });

  return {
    isLogged,
    isAdmin
  };
}, AdminLayout);

export default AdminLayout;

My console.log() return juste function ReactMeteorData() :confused: I don’t know how i can passing my data in my function.

Anyone have idea ?

Thank you community !

Hey dude, it looks like you didn’t fully understand async function calls.

const isAdmin = Meteor.call(‘is-admin’, Meteor.userId(), function(err, data) {
if (err) {
return console.log(err);
} else {
return data;
}
});

return {

isAdmin is never going to get the “data” value in this case. I advise you checking more articles about async calls on google. Here is a good one.

Another thing is here:

const isAdmin = Meteor.call(‘is-admin’, Meteor.userId(), function(err, data) {

You don’t need to send the userId from client to a meteor method, all meteor methods have access to this.userId. More about this here.

Finally, are you using alanning:roles to set user roles? If so you have Meteor.user().roles already in your client, no need to write a method to that :wink:

Hope my answer was helpful. See ya.

You may want to take a look at ES7 async and await for addressing your requirement. This article may help:

Hi everybody,

Thank for your responses. I will edit my const isAdmin. But, how i can add the containerData to my AdminLayout ?

When i’ve data, createContainer must be add data to the props AdminLayout ?

Thank you again

Hi everybody,

Thank you for your help, I finally finished what I wanted to do :

class AdminLayout extends Component {

  constructor(props) {
    super(props);
    this.state = {
      isAuthenticated: Meteor.userId() !== null
    }
  }

  render() {

    const Component = this.props.component;

    if (this.state.isAuthenticated) {

      if (this.props.user && this.props.user.roles.__global_roles__[0]) {

        return (

          <Route render={props => (
            <div>
              <HeaderAdmin />
              <main>
                <Component {...props} />
              </main>
              <FooterAdmin />
            </div>
          )} />

        )

      } else {
        return <div>No admin</div>
      }

    } else {
      return <Redirect push to="/connexion"/>
    }

  }

}

export default createContainer(() => {
  return {
    user: Meteor.user()
  }
}, AdminLayout);

I completely reviewed my code, and studied your resources forwarded previously.

I think it is not optimal yet, it is much better than the first :slight_smile:

Thank you !