Templete renders else block, on reload, renders if

I have the following template, which is loaded when a user clicks o the “Who osted this” button on a particular post.

<template name="profile">

{{#if existinguser}}
`//other code to render when esitinguser returns true//`
    {{else}}
    <p>This user doesn't exist.</p>
    {{/if}}
</template>

Here’s is existinguser helper:

'existinguser': function() {
    

        if (Meteor.users.find({
                "profile.username": document.URL.split('/')[4]
            }).count() > 0) {
            return true;
        } else {
            return false;
        }
    

    }

And the route.js

Router.route('/user/:username', {
    name: 'profile',
    loadingTemplate: 'loading',
    waitOn:function(){Meteor.subscribe('users');
},
    data: function() {
        return Meteor.users.find({
            'profile.username': this.params.username
        });

    
        
    }
});

Now, when the link is clicked, the else block is rendered and the page says This user doesn’t exist, even though it is there in the Users collection.

If I reload the page on the same url, it renders, and shows what ever there is inside the if block.

What might be the problem?

Quick question, have you altered the default Meteor.users document structure? If you are using the default accounts-base and accounts-password, then the username property would not be under profile but rather at the top level of the document.

Something similar to this:

{
  _id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f",  // Meteor.userId()
  username: "cool_kid_13", // unique name
  emails: [
    // each email address can only belong to one user.
    { address: "cool@example.com", verified: true },
    { address: "another@different.com", verified: false }
  ],
  ...
}

If whatever that you have is working for you then this would be a classic case of the Publish and Subscribe timing, which iron-router's waitOn options should handle. Anyways, if you need further help, may be a meteor pad example app would help in solving your problem.

The issue has been resolved. It was because the link was firing without a complete rendering, it was just transporting, not loading.