question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with getter and attributes

See original GitHub issue

I have a model defined roughly as:

sequelize.define("modelName", {
        id       : {
            type       : DataTypes.STRING,
            field      : "mn_id",
            primaryKey : true,
            get        :  function() {
                let id = this.getDataValue("id");
                return id.slice(7);
            },
        }
    });

And I am running a find all against this model like such:

ss.db.AdjustType.findAll({
    attributes : [["mn_id", "name"]],
});

this throws an error because it is attempting to use the getter but the data value is named “name” instead of “id”, so it fails. Is there a way to map these values so the getter works?

I am using version 3.10.0

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
ErnestBrandicommented, Nov 17, 2018

I’m not sure what has been decided about that issue. Customs getters still are called even if a specific list of attributes is set in a findAll({}) call for instance (not dealing with VIRTUAL types here). So what, do I have to check in every getter for undefined values ?!

1reaction
greghartcommented, Mar 2, 2016

Something to note in deciding whether to approach solution C. Even were you to follow solution B, you’d still end up with un-wanted properties in your resulting object

Sequelize = require('sequelize')
db = new Sequelize('test', null, null, {dialect: 'sqlite'})

Test = db.define("test", {
    name: { type: Sequelize.STRING },
    first_name: {
        type: Sequelize.VIRTUAL,
        get: function () {
            if (this.getDataValue('name') !== undefined) {
                return this.getDataValue('name').split(' ')[0]
            } else {
                return undefined;
            }
        }
    }
});


Test.sync()
.then(function(){
    return Test.create({
        name: 'Billy Billyson'
    })
})
.then(function(){
    return Test.findOne({attributes: ['id']})
})
.then(function(user){
    console.log(user.get()) // {first_name: undefined ...} <- unexpected property
})

Seems to contradict expected behavior as outlined https://github.com/sequelize/sequelize/pull/1460

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why getter and setter methods are evil - InfoWorld
Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access...
Read more >
Getters and Setters: Manage Attributes in Python
The problem with regular attributes is that they can't have an internal implementation because they're just variables. So, changing an ...
Read more >
Avoid getters and setters whenever possible
The argument is that getters/setters and properties VIOLATE ENCAPSULATION by exposing internals as properties rather than mutating through ...
Read more >
java - Why getter method doesn't return any object's attribute?
My program should return the different attributes for two different objects. In my main method I set those attributes as the arguments while ......
Read more >
3. Properties vs. Getters and Setters | OOP - Python Courses eu
These methods are of course the getter for retrieving the data and the setter for changing the data. According to this principle, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found