重定向到仪表板页面时出现错误。该错误是在Navbar组件中。我已经使用过ReactMeteorData mixin。我的代码是

const ReactMeteorDataWrap = (BaseComponent)=>{
    return class ExportClass extends Component {
        getMeteorData(){
            let data = {};
            console.log(Meteor.user()); // shows undefined in console
            data.currentUser = Meteor.user();
            console.log('data',data);
            return data;
        }
        render(){
            return <BaseComponent getMeteor={()=>this.getMeteorData()}
                 {...this.props}></BaseComponent>
        }
    }
}

export default ReactMeteorDataWrap;


Navbar.jsx使用ReactMeteorData混合

import ReactMeteorDataWrap from '../ReactMeteorDataWrap.jsx';

class Navbar extends Component {
    constructor(props){
        super(props);
        this.state = { searchText: '' };
        this.props.getMeteor();
    }
     //getMeteorData() {
    //  this.props.getMeteor();
    //}


    componentDidMount(){
        var users = Meteor.users.find({},{fields:{'profile':1}}).fetch();
        var usernames = [];
        users.map(function(user){
            usernames.push(user.profile.fullname);
        });
        $('#typeahead').typeahead({
            name: 'users',
            local: usernames
        });
    }

    handleSubmit(e){
        e.preventDefault();
        FlowRouter.go('/user/' + (this.refs.searchText.value).trim());
    }

    render() {
         var fullname = '';
        if(this.data.currentUser && this.data.currentUser.profile){
            fullname = this.data.currentUser.profile.firstname + ' ' + this.data.currentUser.profile.lastname; // error is shown here
        }
        return ();
}

export default ReactMeteorDataWrap(Navbar);


我在ReactMeteorDataWrap中合并Meteor.user()时得到未定义,并且无法读取Navbar.jsx渲染函数中的属性“ currentUser”。

最佳答案

AFAI可以看到您应该从currentUser而不是this.props提取this.data

render() {
  var fullname = '';
  var currentUser = this.props.currentUser;
  if(currentUser && currentUser.profile) {
    fullname = `${currentUser.profile.firstname} ${currentUser.profile.lastname}`;
    // use fullname for something...
  }
  return;
}

关于javascript - 无法读取未定义的属性“currentUser”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37494401/

10-11 12:29