0

I am building an app in Meteor. I am trying to pass a user object into a template, but the console keeps printing out "user undefined" although I clearly define the user object in my controller:

controller.js:

checkoutController = RouteController.extend({
waitOn: function () { 
    var sessionId = Session.get('sessionId');
    Meteor.subscribe('cartItems', sessionId);
    Meteor.subscribe('userProfileExtended2');
},
data: function(){
    return {
        user: Meteor.users.findOne(this.params._id) //user defined
    };
},
action: function() {
    this.render('Checkout');
    console.log('user:')
    console.log('user: ' + user);
}
});

My publish.js, which publishes the user object:

//this is for a user to edit his/her own profile since there's no admin check

Meteor.publish('userProfileExtended2', function(userId) {
if(!this.userId) return null;
var currUser = Meteor.users.find(this.userId);

//need to add logic to make sure user can only edit his/her account
console.log('user searched for from userprofileextended2 is ' + this.userId);
return Meteor.users.find(this.userId);
});

Here is my route:

Router.route('/checkout', {name: 'Checkout', controller: 'checkoutController'});

Notes:

1) My terminal console prints out the user Id correctly 2) My browser console prints out "ReferenceError: user is not defined"

Any thoughts? Thank you in advance!

3
  • change the data from the object context to this data:function(){ user = Meteor.users.findOne(this.params._id)} Commented Apr 14, 2015 at 15:28
  • Thanks - I'm still running into that issue though :/ Commented Apr 14, 2015 at 15:34
  • hi - console.log('user: ' + user); is already after the this.render('Checkout'); Commented Apr 14, 2015 at 16:27

1 Answer 1

1

You have a couple of issues in your code which can cause confusion, namely you are returning the wrong user in the subscription function. The second is you need to specify which user you want to see:

Meteor.publish('userProfileExtended2', function() {
    return Meteor.users.find({_id: this.userId});
});

You also need to print the correct variable when you render the page:

console.log('user: ' + Meteor.user())

You don't need to use search for the user. You can use Meteor.user() in code or {{currentUser}} in your templates.

Also when you send down the user's document using Meteor.users.find be careful to use projections so that only the fields you intend to share are provided and not the entire document. This can enhance the security of the user's document being viewed.

Sign up to request clarification or add additional context in comments.

2 Comments

hi - thank you for your answer. however, i tried it and i am still getting the same error.. is there any other information i can give you to help me?
@user1547174 Thank you for your clarification/edit. I have updated the answer you can use Meteor.user() nothing more is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.