TypeError: Cannot read property 'length' of undefined
See original GitHub issueWhat are you doing?
try {
await User.create({
nickname: 'super',
admin: UserAdmin.ADMIN,
password: '123456'
});
} catch (error) {
console.log(error);
}
db.close();
To Reproduce Steps to reproduce the behavior:
- Define a model User .
- Call the static method
create
- But see an error
TypeError: Cannot read property 'length' of undefined
at User._initValues (/Users/pedro/projects/nodejs/cms/lin-cms-koa/node_modules/sequelize/lib/model.js:140:49)
at new Model (/Users/pedro/projects/nodejs/cms/lin-cms-koa/node_modules/sequelize/lib/model.js:118:10)
at new User (/Users/pedro/projects/nodejs/cms/lin-cms-koa/node_modules/lin-mizar/lin/core.js:181:1)
at run (/Users/pedro/projects/nodejs/cms/lin-cms-koa/tests/helper/add_super.js:7:18)
at Object.<anonymous> (/Users/pedro/projects/nodejs/cms/lin-cms-koa/tests/helper/add_super.js:24:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
What do you expect to happen?
create an user successful!
What is actually happening?
there was an error! and happened on this line:
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
// do after default values since it might have UUID as a default value
if (this.constructor.primaryKeyAttributes.length) {
this.constructor.primaryKeyAttributes.forEach(primaryKeyAttribute => {
if (!Object.prototype.hasOwnProperty.call(defaults, primaryKeyAttribute)) {
defaults[primaryKeyAttribute] = null;
}
});
}
when creating an user, the constructor.primaryKeyAttributes is undefined,so why this caused.
Output, either JSON or SQL
no sql and no json, just crashed on the sequelize , the core file is sequelize/lib/model.js
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any Dialect library version: 1.6.5 Database version: 5.7.24 Sequelize version: 5.3.5 Node Version: v10.15.3 OS: mac 10.14.5 If TypeScript related: TypeScript version: XXX Tested with latest release:
- No
- Yes, specify that version:
Issue Analytics
- State:
- Created 4 years ago
- Comments:11
Top Results From Across the Web
Cannot read properties of undefined (reading 'length') - TrackJS
This message indicates that our code expects to have an object with a length property, but that object was not present. length is...
Read more >Typeerror: Cannot Read Property 'length' of Undefined
A typeerror length of undefined can indicate that the actual type of an operand is different from the expected type. This error can...
Read more >[SOLVED] Cannot Read Property 'length' of Undefined in JS
To fix the “cannot read property 'length' of undefined” error, perform an undefined check on the variable before accessing the length property ......
Read more >Cannot read property length of undefined - Stack Overflow
The error means that what ever element is returned by getElementById('WallSearch') , it does not have a value property. Every form field has...
Read more >TypeError: Cannot read property 'length' of Undefined in JS
The "Cannot read property 'length' of undefined" error occurs when accessing the length property on an undefined value. To solve the error, make...
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
solved!
@nolan-m Check your connection and that your models are registered.