Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this returns undefined inside of getterMethods object #5304

Closed
erikakers opened this issue Jan 25, 2016 · 6 comments
Closed

this returns undefined inside of getterMethods object #5304

erikakers opened this issue Jan 25, 2016 · 6 comments

Comments

@erikakers
Copy link

Used Sequelize CLI generate a new project and using the CLI to create models and migrations and created a Players model:

module.exports = (sequelize, DataTypes) => {
    const Player = sequelize.define('Player', {
        firstName: {
            type: DataTypes.STRING
        },

        lastName: {
            type: DataTypes.STRING
        }
    }, {
        getterMethods: {
            fullName: () => {
                return this.firstName + ' ' + this.lastName;
            }
        },
        classMethods: {
            associate: (models) => {
                // associations can be defined here
            }
        }
    });

    return Player;
};

Trying to create the fullName inside of the returns an error:

Unhandled rejection TypeError: Cannot read property 'firstName' of undefined
    at [object Object].fullName (player.js:68:24)
    at [object Object].Instance.get (/vagrant/api/node_modules/sequelize/lib/instance.js:186:39)
    at [object Object].Instance.get (/vagrant/api/node_modules/sequelize/lib/instance.js:209:31)
    at [object Object].Instance.toJSON (/vagrant/api/node_modules/sequelize/lib/instance.js:1050:15)
    at [object Object].<anonymous> ({player_id}.js:9:34)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

If I do console.log(this); within the getterMethods it always returns undefined. I've tried to few different combinations to get access to the values but I can't get anything to work. What am I missing?

@Joshua-F
Copy link

I believe it might be due to you using the new arrow function syntax, which actually changes how this behaves compared to just function(). Try changing () => { to function() {.

Click here for more info on how this behaves differently with arrow functions.

@mickhansen
Copy link
Contributor

Yep, using arrow functions incorrectly.

@erikakers
Copy link
Author

Drep, totally overlooked that.

@DiegoRBaquero
Copy link

Took me an hour to find this. I wasn't googling right I guess.

Thank you.

@BradlySharpe
Copy link

Also took me a while to find this :(

Have found instead of replacing arrow functions with method: function() { return this.id; } you can use

getterMethods: {
    method() {
        return this.id
    },
}

@snickroger
Copy link

Wowwwww. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants