.toJSON not including getterMethods
See original GitHub issueRecently 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:
- Created 4 years ago
- Reactions:4
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
I’ve noticed if you add a VIRTUAL field
fullName
with no get function defined, then the getterMethod appears to work…Not sure if this is good/recommended practice?