You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@gideaoms Yes, this is why I added a way for anybody to get the same results as we have and see if it is something wrong with our setup or not. Makes it easier for a maintainer to understand the issue and be able to debug if needed.
Ok I did some more investigating and I think in my example, it boils down to using babel-node vs ts-node.
In here: jrm2k6/laughing-octo-enigma@e745244
I changed my tsconfig to use commonjs instead of esnext, and use ts-node instead of babel-node.
When I run node -r ts-node/register index.ts it lets me access the model properties directly. I am not 100% sure yet but I think with the way my babel-node config is set up, it will generate model properties automatically initialized with undefined, overriding what sequelize-typescript might be doing when it comes to accessing model properties. Still a guess though.
Encountered this issue when upgrading typescript from 3 to 4. Data was only in dataValues but not in the class fields. Setting useDefineForClassFields to true in my tsconfig seems to have fixed it.
Reacted by Giovanni Patruno, Arrigo Zanette, Brad Reardon, tanfeng, Arnaud LYARD, Dario Delle Donne, Herless Alvarado, Danyil Moroz, KsoftM, Ibsen Fahir and 2 more
The new documentation is a big improvement over where we were at a week ago, unfortunately the declare keyword trick only works with tsc tooling and not with babel due to the above linked PR.
The fact that babel still emits public class fields tagged with declare when using decorators is problematic. Makes sequelize/sequelize#14300 all the more pressing.
I have worked around this in my project with a model class decorator – it's very ugly, but the approach could be used as another way to fix the issue more formally in sequelize by applying the getter/setters after the model constructor.
First of all great project! Strange that README.md not updated with 'declare' workaround, as well as with ReturnType<() => Model>; for associated fields.
To prepare PR with "declare" column attributes and "ReturnType" associated attributes?
It seems that setting "lib": ["ES2023"] causes this issue. Upon some investigation, it actually seems like setting lib to this value may switch on useDefineForClassFields. After I forced useDefineForClassFields off, the issue went away. I would not consider this a resolution though, because it seems like the wrong decision long term.
TSConfig:
we just ran into this issue after replacing webpack with rspack and I didn't even notice it was a problem until I ran a query and everything was undefined. We've got around 200 models and thousands of fields and if we try to make the "declare" change, there's going to be at least 1 field that makes it's way to production either in the initial push for it, or someone's rebase.
Unless there's an eslint rule that can prevent it from reaching production I think it's quite dangerous that it's only a warning at runtime and for it to essentially silently fail. Nobody is going to notice an undefined property for days/weeks/months after hours of debugging
I'd love to go with this but it's too dangerous to try. Has anyone found an eslint rule that can enforce it on all properties that extend Model perhaps? TypeScript only prevents it on the reserved fields: id, createdAt, updatedAt etc, but the custom fields are only added magically by sequelize.
Activity
wdzeng commentedon Jan 13, 2021
Same problem. It seems like I need to call
getDataValue.jrm2k6 commentedon Feb 22, 2021
I created a quick repo here showing the issue: https://github.com/jrm2k6/laughing-octo-enigma
When running the command to execute a findAll this is what I get back:
I also use sequelize-typescript with nestjs and I am not having the same issue but haven't found out if it does something different.
Hope that helps so that we can figure out if we are doing something wrong or not.
Thanks!
gideaoms commentedon Feb 22, 2021
@jrm2k6 The problem is I can not get the value just using
user.name, but if I useuser.getDataValue('name')works fine.I tested it with your github project and the result was the same.
jrm2k6 commentedon Feb 22, 2021
@gideaoms Yes, this is why I added a way for anybody to get the same results as we have and see if it is something wrong with our setup or not. Makes it easier for a maintainer to understand the issue and be able to debug if needed.
jrm2k6 commentedon Feb 28, 2021
Ok I did some more investigating and I think in my example, it boils down to using babel-node vs ts-node.
In here: jrm2k6/laughing-octo-enigma@e745244
I changed my tsconfig to use commonjs instead of esnext, and use ts-node instead of babel-node.
When I run
node -r ts-node/register index.tsit lets me access the model properties directly. I am not 100% sure yet but I think with the way my babel-node config is set up, it will generate model properties automatically initialized with undefined, overriding what sequelize-typescript might be doing when it comes to accessing model properties. Still a guess though.Here we go: #612
gideaoms commentedon Mar 1, 2021
Yep, it makes sense.
triesmon commentedon Sep 23, 2021
Encountered this issue when upgrading typescript from 3 to 4. Data was only in dataValues but not in the class fields. Setting useDefineForClassFields to true in my tsconfig seems to have fixed it.
nathanlepori commentedon Sep 30, 2021
That's weird. For me doing the exact opposite (setting useDefineForClassFields to false) fixed it. I'm targeting ESNext on Node 14.
triesmon commentedon Sep 30, 2021
I had actually set it to false but just wrote the wrong value in the comment. Thanks.
devuxer commentedon Oct 19, 2021
For me, it had nothing to do with babel (which I wasn't using to begin with).
The solution was entirely to add
"useDefineForClassFields": "false"to my tsconfig.json.Thanks, @triesmon!
tommoor commentedon Jan 7, 2022
This appears to be the change (and some surrounding discussion) in babel that breaks this behavior:
babel/babel#11747
ephys commentedon Jan 7, 2022
Take a look at the updated sequelize documentation regarding the issue with class fields: https://sequelize.org/v6/manual/model-basics.html#caveat-with-public-class-fields
tommoor commentedon Jan 7, 2022
The new documentation is a big improvement over where we were at a week ago, unfortunately the
declarekeyword trick only works withtsctooling and not with babel due to the above linked PR.ephys commentedon Jan 7, 2022
The fact that babel still emits public class fields tagged with
declarewhen using decorators is problematic. Makes sequelize/sequelize#14300 all the more pressing.tommoor commentedon Jan 7, 2022
I have worked around this in my project with a model class decorator – it's very ugly, but the approach could be used as another way to fix the issue more formally in sequelize by applying the getter/setters after the model constructor.
https://github.com/outline/outline/blob/main/server/models/decorators/Fix.ts
keysmusician commentedon Apr 28, 2023
I had this problem as well in Deno 1.32 which doesn't support
useDefineForClassFields. I fixed it by justdeclare-ing all my fields:d00rsfan commentedon Jul 5, 2023
First of all great project! Strange that README.md not updated with 'declare' workaround, as well as with ReturnType<() => Model>; for associated fields.
To prepare PR with "declare" column attributes and "ReturnType" associated attributes?
kbirger commentedon Apr 16, 2024
I landed here with a similar issue. The issue arose when I updated my tsconfig to the recommended settings for node 20
It seems that setting
"lib": ["ES2023"]causes this issue. Upon some investigation, it actually seems like settinglibto this value may switch onuseDefineForClassFields. After I forceduseDefineForClassFieldsoff, the issue went away. I would not consider this a resolution though, because it seems like the wrong decision long term.TSConfig:
{ "compilerOptions": { "allowJs": false, "baseUrl": "./", "declaration": true, "useDefineForClassFields": false, "emitDecoratorMetadata": true, // "esModuleInterop": true, "experimentalDecorators": true, "forceConsistentCasingInFileNames": true, "incremental": true, "module": "CommonJS", "moduleResolution": "Node", "noImplicitReturns": true, "noUnusedLocals": true, "noUnusedParameters": true, "outDir": "./dist", "removeComments": true, "skipLibCheck": true, "strict": true, "target": "ES2017", "lib": [ "ES2023" ], "typeRoots": [ "node_modules/@types", "src/types" ], "resolveJsonModule": true, "sourceMap": true, "inlineSources": true, "sourceRoot": "/", "allowSyntheticDefaultImports": true }, "exclude": [ "node_modules", "dist", "knex/templates", ] }Node: 20.11.0
TypeScript: 5.4.5 (although issue appeared on older version too)
Sequelize: 6.37.3
Sequelize-TypeScript: 2.1.6
mopcweb commentedon Apr 23, 2024
Thanks so much! Spent a lot of time trying to find out a problem, this solved my issue!
intellix commentedon Oct 13, 2024
we just ran into this issue after replacing webpack with rspack and I didn't even notice it was a problem until I ran a query and everything was undefined. We've got around 200 models and thousands of fields and if we try to make the "declare" change, there's going to be at least 1 field that makes it's way to production either in the initial push for it, or someone's rebase.
Unless there's an eslint rule that can prevent it from reaching production I think it's quite dangerous that it's only a warning at runtime and for it to essentially silently fail. Nobody is going to notice an undefined property for days/weeks/months after hours of debugging
I'd love to go with this but it's too dangerous to try. Has anyone found an eslint rule that can enforce it on all properties that extend Model perhaps? TypeScript only prevents it on the reserved fields: id, createdAt, updatedAt etc, but the custom fields are only added magically by sequelize.