Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'length' of undefined #11111

Closed
2 of 7 tasks
pedrogao opened this issue Jun 25, 2019 · 11 comments
Closed
2 of 7 tasks

TypeError: Cannot read property 'length' of undefined #11111

pedrogao opened this issue Jun 25, 2019 · 11 comments

Comments

@pedrogao
Copy link

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:
@pedrogao
Copy link
Author

solved!

@nolan-m
Copy link

nolan-m commented Jun 27, 2019

@pedrogao What was the solution? Running in this myself.

@BobGneu
Copy link

BobGneu commented Jun 30, 2019

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

@MahmoudAbdo90
Copy link

@pedrogao @nolan-m @BobGneu
running into the same issue, on Model.create
anyone got it working ?

@nolan-m
Copy link

nolan-m commented Oct 23, 2019

@MahmoudAbdo90
I fixed mine by making sure the models where initialized before using them.

I had some unit tests that used Sequelize models, but I never loaded the file that initialized all them in the unit tests.

@MahmoudAbdo90
Copy link

I figured the problem out, It was a configuration problem.
@nolan-m Thanks man

@Valves133
Copy link

I was having the same problem. Because I was importing my Model as an instance. I just imported the class alone normal and if it was the error

@Cardoso-CHM
Copy link

Solved here!

I just forgot to add my new model inside my "models" array inside sequelize class configuration file, in my project this file is located at: src/database/index.js

@andersonlthome
Copy link

I did the same as @Cardoso-CHM

@cosmoarunn
Copy link

@pedrogao Thanks!

I was running into this problem and very much troubled to find the solution
few things can be the primary reason, the seq authentication check, await the sync and model initialization
here's the working sample:

try {
      await sequelize.authenticate();  //do authentication
      await User.initialize(sequelize);  /**/initialize user model**
      console.log('Connection has been established successfully.');
    } catch (error) {
      console.error('Unable to connect to the database:', error);
      return response('Database connection error!')
    }

and the User Model,

//define the class
class User extends Model {}
User.init({
  username: DataTypes.STRING,
  birthday: DataTypes.DATE
}, { sequelize, modelName: 'user' });

and create,

(async () => {
  await sequelize.sync();   // call the sync before creating
  const jane = await User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  });
  console.log(jane.toJSON());
})();

@johnatan-etges
Copy link

Solved here!

I just forgot to add my new model inside my "models" array inside sequelize class configuration file, in my project this file is located at: src/database/index.js

This was the solution to me! Many tanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants