reactjs - MeteorJS: Get user profile on client side -


so, problem has been documented, yet here am, stuck again...

on server side, have publish:

meteor.publish('userdata', function(){ if (!this.userid) { return null; }  return meteor.users.find({}, {fields: {'profile': 1}}); }); 

on client side router subscriptions, have:

this.register('userdata', meteor.subscribe('userdata')); 

and in client code, have:

if (meteor.userid()) {   var profile = meteor.users.find(meteor.userid()).profile;   console.log(profile); // keep getting undefined... 

i not using autopublish or insecure package.

my data in mongodb collection looks like:

{"_id" : "...", profile: { "password" : { "network" : "...", "picture" : "none" } } 

my error says:

uncaught typeerror: cannot read property 'password' of undefined 

thoughts?

the user profile automatically published client, don't need write custom publication send it. that's why should never store sensitive user information in profile. can rid of publication.

to access profile in component, need wrap component (or entire app) in data container sends profile component. reason container reactive, component load until meteor user object ready, can access stuff stored in profile.

create container:

import { meteor } 'meteor/meteor'; import { createcontainer } 'meteor/react-meteor-data'; import { navigation } '/imports/ui/components/user-navigation/navigation';  export default createcontainer(() => {     const loading = !meteor.user();     const user = meteor.user();     return { loading, user }; }, navigation); 

access profile in component:

import react, { component } 'react';  export class navigation extends react.component {     constructor(props) {         super(props);     }      render() {         const { user, loading } = this.props;         return (              loading ? <loading /> :              <div classname="navigation">                 user's name is: { user.profile.firstname }             </div>         )     } }; 

Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -