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.

Getters not being called on find or get({plain: true}) neither

See original GitHub issue

What are you doing?

I’ve got a model with a virtual method called payULink() that produces an URL using the id field of the model. On Sequelize 4.44.0, calling findAll() or get() on an instance calls the methods that produce this virtual property correctly, but on sequelize 5.8.5 it does not.

let purchaseOrder = sequelize.define('purchaseOrder', attributes, {
    getterMethods: {
        payULink() {
           return somethingMagic(this.id);
        }
    }
});

To Reproduce Steps to reproduce the behavior:

  1. Define a model with getters.
  2. Run findAll() on the model or get() on an instance.
  3. See error.

What do you expect to happen?

I was expecting to receive the object with the virtual properties obtained from calling the methods.

What is actually happening?

The object only contains the properties that are not virtual.

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any Dialect library version: mysql2 1.6.5 Database version: 5.7.18 Sequelize version: 5.8.5 Node Version: 10.15.3 OS: macOS Mojave If TypeScript related: TypeScript version: XXX Tested with latest release:
  • No
  • Yes, specify that version: 5.8.3

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nawlbergscommented, Dec 4, 2019

They recommend making them virtual

sequelize.define('YerModel', {
   url:{
      type: DataTypes.VIRTUAL,
      get: ...getter function...
   }
});

But the breaking change/bug is that … if your query has an attributes array… the virtual field wont be returned unless specifically requested… see my comment above.

Its worth noting that after making them virtual fields - they will now also show up in the model instances list of fields (model.rawAttributes). i don’t know if you have any code that inspects the fields on your models… but I had to modify some of our rest api to account for the virtual fields showing up as a field. Mainly because our rest api allows you to sort a query if you pass it a valid field… and trying to sort by a virtual field blows up… (which might be another bug)

1reaction
zlwaterfieldcommented, Dec 4, 2019

@papb we have > 50 models (with many getterMethods). This was not documented at all as a breaking change and I feel it should either be fixed or documented. I am curious why getterMethods are being deprecated? What is the best practice instead of them? I know you mentioned using VIRTUAL columns but how do you convert to them from something like this:

    getterMethods: {
      url: function() {
        if (this.cdn_base && this.cdn_id) {
          return `${this.cdn_base}/${this.cdn_id}/-/progressive/yes/`;
        } else {
          return this.legacy_url.indexOf('cdn.domain.com') > -1
            ? this.legacy_url.replace('.com/', '.com/compress/')
            : this.legacy_url;
        }
      },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize getters do not run on model.findall( { raw:true } )
Right, that is what the description says. getter should not be called when raw: true is applied. So if you want the getter...
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
The get syntax binds an object property to a function that will be called when that property is looked up. It can also...
Read more >
Getters, Setters & Virtuals - Sequelize
Sequelize allows you to define custom getters and setters for the attributes of your models.
Read more >
Node.js v19.3.0 Documentation
stack <Object> A stack trace of the function. The arrays contains information about the expected and actual number of calls of the functions...
Read more >
Prototype methods, objects without __proto__
This call makes a truly exact copy of obj , including all properties: enumerable and non-enumerable, data properties and setters/getters ...
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