Skip to content

$lazy: true causing Error in beforeCreate hook: "TypeError: Cannot read property '***' of undefined" #52

Open
@wildhart

Description

@wildhart

If a meteor property relies on any props or other component data or computed properties, it causes an error when used with $lazy: true. It seems that the function is being called within the beforeCreate hook at which point the other data doesn't exist yet.

e.g:

props: ['userId'],
meteor: {
    $lazy: true,
    avatar() {
        console.log(this.userId); // [Vue warn]: Error in beforeCreate hook: "TypeError: Cannot read property 'userId' of undefined"
        const user = Meteor.users.findOne({_id: this.userId}, {fields: {"profile.avatar": 1}});
        return Meteor.absoluteURL('avatars/'+user.profile.avatar);
    }
}

My work-around is to do this instead:

props: ['userId'],
computed: {
    avatar() { () => { return this.$autorun(() => {
        console.log(this.userId); // no problem
        const user = Meteor.users.findOne({_id: this.userId}, {fields: {"profile.avatar": 1}});
        return Meteor.absoluteURL('avatars/'+user.profile.avatar);
    })}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions