Search code examples
javascriptmeteor

Cannot read property of undefined error when passing user data into meteor template


I keep getting the following error:

Exception in template helper: TypeError: Cannot read property '_id' of undefined at Object.editable

I've been trying to debug it with no luck.

I'm trying to pass a user object into an editUser template. So when I click edit, and get the form, I'll be able to see the user previous values inside the input fields, but I cannot get the values to appear on the form.

Here is the code I have now:

# users.js

// Helpers
Template.user.helpers({
    user: () => {
        let user =  Meteor.user() ? Meteor.user() : 'Unable to pull user info';
        return user
    },
    editable: function () {
        
        let currentUserId =  Meteor.user()._id;
        let currentUser =  Meteor.user();

        // let currentUserFind = Meteor.users.findOne({_id: this._id});

        return Session.equals('editUser', currentUserId);
    }
});


# user.tpl.jade
// Events
Template.user.events({
    'click .form-edit'(e) {
        e.preventDefault();

        let currentUser=  Meteor.user({});
        let currentUserId =  Meteor.user()._id;
        let currentUserFind = Meteor.users.findOne({_id: this._id});

        console.log(currentUserId); 
        console.log(currentUserFind);   
        // console.log(this._id);
        
        Session.set('editUser', currentUserId);
        // Session.set('editUser', this._id);
    }
});

if editable
    +editUser
else
    with currentUser
        .card.card--user
            h3.card__title= username
            p.card__content= profile.email
            p.card__content= profile.firstName
            p.card__content= profile.lastName
            p.card__content= profile.bio
            button.card__btn.form-edit Edit

I have tried passing the id and the user object but I cannot get the error to disappear.


Solution

  • I have seen this exception with Meteor.user()._id when you don't import the Meteor pseudo-global. Once you import {Meteor} the exception will disappear. Meteor.userId() will also do the work.