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

.toJSON not including getterMethods #10997

Closed
W0mpRat opened this issue May 23, 2019 · 13 comments
Closed

.toJSON not including getterMethods #10997

W0mpRat opened this issue May 23, 2019 · 13 comments
Labels
existing workaround For issues. There is a known workaround for this issue. status: in discussion For issues and PRs. Community and maintainers are discussing the applicability of the issue. type: bug

Comments

@W0mpRat
Copy link

W0mpRat commented May 23, 2019

Recently upgraded to v5 from v4. Version 5.8.6

User model with fullName getterMethod

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    firstName: {
      type: DataTypes.STRING,
      allowNull: false
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    getterMethods: {
      // TODO: Not working in latest sequelize
      fullName () {
        const fullName = `${this.firstName} ${this.lastName}`

        return fullName
      }
    },
    paranoid: true
  })

  User.associate = (models) => {
    User.belongsToMany(models.Project, {
      through: models.UserProject,
      foreignKey: 'userId'
    })
    User.belongsTo(models.Company, {
      foreignKey: 'companyId',
      onDelete: 'CASCADE'
    })
  }

  return User
}

Controller endpoint:

  async show (req, res) {
    try {
      const result = await User.findByPk(req.params.id, {
        include: [{
          through: {
            attributes: ['default']
          },
          model: Project
        }]
      })

      res.status(200).send(result)
    } catch (error) {
      sendTeamsError(error)
      res.status(400).send(error)
    }
  },

Bug:

result has fullName property but result.toJSON() does not include fullName

@pi-kei
Copy link
Contributor

pi-kei commented May 28, 2019

That's because of this breaking change #9568
Exactly because of this new if-statement

sequelize/lib/model.js

Lines 3318 to 3323 in 2c50b7e

if (
this._options.attributes
&& !this._options.attributes.includes(_key)
) {
continue;
}

If you add VIRTUAL field fullName instead of getter, then request it in attributes ['firstName', 'lastName', 'fullName'], it would work.
But with getter it would fail.

@mjy78
Copy link
Contributor

mjy78 commented Jul 9, 2019

I've noticed if you add a VIRTUAL field fullName with no get function defined, then the getterMethod appears to work....

const User = sequelize.define('User', {
  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  fullName: {
    type: DataTypes.VIRTUAL
  }    
  }, {
  getterMethods: {
    fullName () {
      return `${this.firstName} ${this.lastName}`;
    }
  }
})

Not sure if this is good/recommended practice?

@papb
Copy link
Member

papb commented Jul 24, 2019

@mjy78 Once this gets fixed, it won't be a good practice. But for now it's a valid workaround, I'd say. Stay tuned.

@papb papb self-assigned this Jul 24, 2019
@papb papb added dialect: all existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. type: bug labels Jul 24, 2019
@jayarjo
Copy link

jayarjo commented Aug 15, 2019

@papb and how it is going to work, once it's fixed?

@mjy78 does it work if you have get on the VIRTUAL field and no corresponding getterMethod?

@papb
Copy link
Member

papb commented Aug 15, 2019

@papb and how it is going to work, once it's fixed?

@jayarjo When I self-assigned this, my idea was to make every getterMethod appear in the .toJSON() output, just that, very simple. But now I realize this might be technically considered a breaking change...

@mjy78 does it work if you have get on the VIRTUAL field and no corresponding getterMethod?

Yes, it works. I personally prefer this way, even if the other way worked already... Looks cleaner to me (but this is just my opinion).

@sushantdhiman Perhaps we could just deprecate getterMethods in favor of virtual fields? What do you think?

@papb papb added status: in discussion For issues and PRs. Community and maintainers are discussing the applicability of the issue. and removed status: understood For issues. Applied when the issue is understood / reproducible. labels Aug 15, 2019
@jayarjo
Copy link

jayarjo commented Aug 15, 2019

I also prefer VIRTUAL fields, feels cleaner. You should definitely consider deprecating one or the other.

@gabegorelick
Copy link
Contributor

It would be nice if this was documented in https://github.com/sequelize/sequelize/blob/master/docs/upgrade-to-v5.md, especially if a fix may take a while.

@papb papb removed the dialect: all label Sep 17, 2019
@cperryk
Copy link

cperryk commented Oct 2, 2019

This is also happening with instance.get({ plain: true }) — the resulting object does not include the result of any getters defined via the model's getterMethods

@hverlin
Copy link

hverlin commented Jun 18, 2020

This should definitely be mentioned in the breaking changes. Happy to update the documentation if that's ok?

@redevill
Copy link

When the getters ARE invoked, and they return undefined, it appears that the toJSON() falls on its face!

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 10, 2021
@W0mpRat
Copy link
Author

W0mpRat commented Nov 10, 2021

bump

@ephys
Copy link
Member

ephys commented Jun 28, 2023

getterMethods and setterMethods were deprecated in v6 and have been removed in v7: https://sequelize.org/docs/v7/other-topics/upgrade/#removal-of-previously-deprecated-apis

@ephys ephys closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
existing workaround For issues. There is a known workaround for this issue. status: in discussion For issues and PRs. Community and maintainers are discussing the applicability of the issue. type: bug
Projects
None yet
Development

No branches or pull requests

10 participants