0

This is my model class. It is located in a file called user.js:

const { Sequelize, Model, DataTypes} = require('sequelize');


class User extends Model{
    

    static Init(sequelize){
        var model = sequelize.define('user',{
            id: {type:Sequelize.UUID, defaultValue: Sequelize.UUIDV4, primaryKey:true},
            username: {type:DataTypes.STRING, allowNull:false},
            password: {type:DataTypes.STRING, allowNull:false},
            name: {type:DataTypes.STRING, allowNull:false},
        });
        return model;
    }

    GetName(){
        return this.name;
    }

};

module.exports = { User };

When I try to call the Sequelize build function from my app.js, I get this error:

C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\node_modules\sequelize\lib\model.js:130 if (this.constructor.primaryKeyAttributes.length) { ^

TypeError: Cannot read properties of undefined (reading 'length') at User._initValues (C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\node_modules\sequelize\lib\model.js:130:49) at new Model (C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\node_modules\sequelize\lib\model.js:116:10) at new User (C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\modals\user.js:4:1) at Function.build (C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\node_modules\sequelize\lib\model.js:1311:12) at initialize (C:\Users\testenv\Documents\Development\NodeJS\simple-express-project\app.js:26:19) at processTicksAndRejections (node:internal/process/task_queues:96:5)

I have traced it to the app.js line 26 which is:

var nu = User.build({
        username: "test"
})

I can't seem to access the properties of User in the build as well.

halfer
  • 19,824
  • 17
  • 99
  • 186
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • If you extending Model class then use `User.init` instead of calling `define`, see https://sequelize.org/master/manual/model-basics.html#extending--a-href-----class-src-model-js-model-html--model--a- – Anatoly Feb 24 '22 at 17:38
  • Hi @Anatoly, but if I use User.init, how do I reference it outside the class? – JianYA Feb 25 '22 at 13:46
  • Referenece what exactly? – Anatoly Feb 25 '22 at 15:16
  • Sorry I was trying to get help for another question of mine here: https://stackoverflow.com/questions/71263538/express-js-how-to-reference-a-sequelize-class – JianYA Feb 25 '22 at 15:28

0 Answers0