question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'length' of undefined

See original GitHub issue

What 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:

  1. Define a model User .
  2. Call the static method create
  3. 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:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

68reactions
pedrogaocommented, Jun 25, 2019

solved!

29reactions
BobGneucommented, Jun 30, 2019

@nolan-m Check your connection and that your models are registered.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found