0

I'm struggling to pass data from one template to another, and thinking about it I wonder if that's my problem anyway. I'm using the built in accounts system, I have adding new users and authentication working, and when a user signs in I forward them to a new template. I'd like to be able to use their details in that template but I'm struggling to figure out the best way to do this.

Initially I thought I could simply use: Router.go('userPage', {user:username}); which gives no errors but doesn't work. In my template I'm using : <p>Welcome {{user}}</p>

Using {{> user}} throws an 'Can't find template, helper or data context key: username' error.

Any ideas?

EDIT: Ignore the rest, after restarting the Meteor server this is now working.

Thinking I have a bigger issue here so adding more detail:

Taking some info from a form and then routing based upon the outcome:

Meteor.loginWithPassword(username, password, function(err) {
            if (err) {
                console.log('Logging in failed');
            } else {
                console.log('Logging in succeeded');
                console.log(username);
                Router.go('userPage');
            }
        });

I then wish to open this template and pass through the data of the user who just logged in:

<template name="userPage">
    <div class="container">
        <h1>Welcome {{username}}</h1>
    </div>
    <p>User Page</p>
</template>

If I use <p>Welcome {{currentUser.username}}</p> I get no errors but also no name. Meteor.user().username does return a name but I cannot get that into the template.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tortex
  • 163
  • 1
  • 1
  • 12

1 Answers1

0

"username" is undefined, you should use Meteor.user().username

Or better yet, you should use the currentUser predefined helper which contains the currently logged in user that you can pass between pages.

<p>Welcome {{currentUser.username}}</p>

The usual way to pass data between templates is by transmitting the document _id in the route url then retrieving it with the Iron Router and doing your stuff with the doc.

Sometimes it is easier to store currentSomething in a global helper, with a reactive Session variable keeping track of the "something _id" but it can lead to pretty sloppy code, beware !

saimeunt
  • 22,666
  • 2
  • 56
  • 61
  • Hmm, still not working. Using:`

    Welcome {{currentUser.username}}

    ` gives me no errors but also doesn't pass any data. If I log `Meteor.user().username` I get a string fine. I think I have a bigger problem going on :) I'll add more detail to my question.
    – Tortex May 24 '14 at 20:09