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.

.toJSON not including getterMethods

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
gabegorelickcommented, Aug 23, 2019

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.

4reactions
mjy78commented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does GSON use fields and not getters/setters?
We mostly saw this when we were trying to make a JSON object to represent method parameters. The workaround for that was to...
Read more >
Significance of Getters and Setters in Java - Baeldung
In this tutorial, we'll discuss the problems of not using getters/setters, ... Returning Object References Directly From the Getter Methods.
Read more >
Getter and Setter in Python - GeeksforGeeks
We use getters & setters to add validation logic around getting and setting a value. To avoid direct access of a class field...
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
Note that attempting to assign a value to latest will not change it. Using getters in classes. You can use the exact same...
Read more >
REST Assured Tutorial 38 – How Getter & Setter methods ...
Converting a Java class object to a JSON payload as string ... If you have only getter methods in POJO class, you can...
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