Meteor.user() error while loading user post

Hi,

I have an exception in my console when I reload the user account page where I want to load all the post of the current user. I guess that it’s due to the asynchronous call to my filter or something like that but I don’t understand how to manage this issue. Because finally my page is loaded like I want but I still have the error in the console…

My router :

`var connectedFilter = function () {
    if (!Meteor.user()) {
        this.render('notFound');
        this.stop();
    }
    else {
        this.next();
    }
};

Router.route('/account', {
    name: "userProfile",
    template: "userProfile",
    before: connectedFilter,
    waitOn: function() {
        var username = Meteor.user().username;
        return Meteor.subscribe("userPost", username);
    },
    data: function(){
        var username = Meteor.user().username;
        var Posts = CPDM.find({author:username}, {});

        return {
            Posts: Posts
        };
    }
});`

The error in the console is this one :

Exception in callback of async function: TypeError: Cannot read property 'username' of undefined
    at waitOn (http://localhost:3000/app/app.js?hash=7b68a15d92da3bfa36869301f6641c97a3c7946d:1722:41)
    at http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:435:22
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:149:11)
    at RouteController._runRoute (http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:434:5)
    at Function.Route.dispatch (http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:850:18)
    at route (http://localhost:3000/packages/iron_router.js?hash=e3e6958b865eb45fe4a72dcf55ec06d87881d90b:707:11)
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?hash=a144d30621d65956143c8d952f54d0082094915f:418:31)
    at http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:22
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?hash=a144d30621d65956143c8d952f54d0082094915f:365:14)

Thank’s for helping !

install fast-render, should fix the issue.
problem probably is your user collection hadn’t downloaded to the client yet.

Can I make something like : waitOn the download of the user collection and then … ??

I believe you need to check if anybody is logged in, otherwise Meteor.user() returns null which would explain this error. Another reason can be a race condition. Iron Router is reactive, so is Meteor.user()

When your waitOn gets called, Meteor.user() might be null. Same in data. Add a console.log(Meteor.user()) and you will see

Router.route('/account', {
    name: "userProfile",
    template: "userProfile",
    before: connectedFilter,
    waitOn: function() {
        var user = Meteor.user();
        if(user) {
        var username = user.username;
        return Meteor.subscribe("userPost", username);
        }
    },
    data: function(){
        var user = Meteor.user();
        if(user) {
        var username = user.username;
        var Posts = CPDM.find({author:username}, {});

        return {
            Posts: Posts
        };
        }
    }
});`

@jamgold It’s actually not working neither because now I’m rendering the notFound Page… What I’m not understanding is why when I come for the first time to the page it’s work but when I’m reloading it, it doesn’t ! And that’s my issue… I thought that the user datas were loading with the user session so they were always available !

Actually Fast Render solved the problem !!! Thank’s a lot.

1 Like