0

I have been trying to do a findall route from my sequelize database models Accounts and Dogs:

db.Accounts.findAll({
            where: {
                admin:true,
            },
            include: [db.Dogs]
        }).then((dbAdminAcc) => res.json(dbAdminAcc));
    });

But when I try to run the app and the routes, it keeps coming back as: TypeError: Cannot read property 'findAll' of undefined. So after doing some console logs in the index.js for the models folder, I discovered that all of the models are coming out in the console as "Model is [function (anonymous)]" for all three models. This makes it impossible for me to access the data at all.

}).forEach((file) => {
      console.log('The file is', file)
      console.log(path.join(__dirname, file))
        const model = require(path.join(__dirname, file));
        console.log('Model is', model) // This is showing a function (anonymous instead of a name like Accounts)
        db[model.name] = model;
    });

    Object.keys(db).forEach((modelName) => {
      console.log('DB with modelname:', (db[modelName]))
        if(db[modelName].associate) {
            db[modelName].associate(db);
       

} });

I looked to see if the syntax was correct in the Dogs and Accounts models to see if the returns are missing, but both models seemed to be set up right:

module.exports = (sequelize, DataTypes) => {
    
    const Dogs = sequelize.define('Dogs', {
        dog_name: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: [2,15],
            },
        },
        breed: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: [2,15],
            },
        },
        age: {
            type: DataTypes.INTEGER,
            allowNull: false,
            validate: {
                len: [2,35],
            },
            isNumeric: true,
            isInt: true,
        },
        food_requirements: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: [2,200],
            },
        },
        friendliness: {
            type: DataTypes.INTEGER,
            allowNull: false,
            validate: {
                len: [1,5],
            },
            isNumeric: true,
            isInt: true,
        }, 
        
    });

    Dogs.associate = (models) => {
        // a Dogs must belong inside the Admin Account
        // Dogs cannot be created without a petId (username) 
        Dogs.belongsTo(models.Accounts, {
            foreignKey: {
                allowNull: false,
            },
        });
    };
    return Dogs;   
};

const bcrypt = require("bcryptjs");

module.exports = (sequelize, DataTypes) => {
    const Accounts = sequelize.define('Accounts', {
      username: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
          isUserName: true
        }
      },
      // The password cannot be null
      password: {
        type: DataTypes.STRING,
        allowNull: false
      },
      //this true or false 
      admin: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
      }
    });
    Accounts.associate = (models) => {
      Accounts.hasMany(models.Dogs, {
          onDelete: 'cascade'
      });
    };

 
  Accounts.prototype.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
  };
 
  Accounts.addHook("beforeCreate", function(accounts) {
    Accounts.password = bcrypt.hashSync(accounts.password, bcrypt.genSaltSync(10), null);
  });
  
  return Accounts;
};

I'm at a loss as to what to try next. Would anybody have any insight as to why this is happening and how to fix it? Any help would be greatly appreciated. Thank you

GKFitz
  • 1
  • 1
    You are missing the function call with arguments i.e. functionName(arg1, arg2). Just make the require as `const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);` Refer: https://stackoverflow.com/a/62984731/8133717 – Abhishek Shah Mar 02 '21 at 18:53

1 Answers1

0

You should register models by using Sequelize import function instead of just importing them using require:

var model = sequelize['import'](path.join(models, file))
db[model.name] = model

UPD: This import method has been deprecated since v6. Use an approach @Abhishek indicated in his comment. Look at my answer here

Anatoly
  • 20,799
  • 3
  • 28
  • 42