'Why does 'this.username' in my Sequelize model return a 'Potentially invalid usage of this' warning but other fields do not?
If I rename the username attribute, the error goes away. 'username' sets this warning off for whatever reason though, which to me is a bit of a headscratcher. I am using IntelliJ Ultimate. Is this perhaps just IDE specific and am I safe to ignore that warning?
username: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [2, 32],
notNull: true,
},
get() {
return this.getDataValue('username');
}
},
fullUsername: {
type: DataTypes.VIRTUAL,
get() {
return `${this.username}#${this.discriminator}`;
},
set() {
throw new Error("user field is not settable.");
}
},
discriminator: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
min: 4,
max: 4,
notNull: true,
}
},
Solution 1:[1]
Inside getters you should always use getDataValue to avoid potential recursive calls when one getter uses another one and vice versa.
fullUsername: {
type: DataTypes.VIRTUAL,
get() {
return `${this.getDataValue('username')}#${this.getDataValue('discriminator')}`;
},
set() {
throw new Error("user field is not settable.");
}
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anatoly |
