'Sequelize .save not working with custom array getter/setter
So I'm using an edited version of How can I have a datatype of array in mysql Sequelize instance? to save arrays in my database.
E.g.
likes: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '[]',
get() {
return JSON.parse(this.getDataValue('likes'))
},
set(val) {
console.log(JSON.stringify(val))
this.setDataValue('likes',JSON.stringify(val));
}
}
Then I'm editing the "likes" value with something similar to this:
function updootChange(postId, creatorId){
// CreatorId here is the person to up/down doot for. Yes i copied the reply function and changed it.
return new Promise((res,rej)=>{
Post.findOne({
where: {
id: postId
}
}).then(async(r)=>{
if(r.likes.includes(creatorId)){
r.likes = r.likes.filter(item => item !== creatorId)
await r.save();
res(-1)
}else{
r.likes.push(creatorId)
await r.save();
res(1)
}
}).catch((e)=>{
console.log(e);
rej()
})
})
}
And although there is no errors, the likes array is never changed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
