简体   繁体   中英

TypeError: Cannot read properties of undefined (reading 'findAll')

I am getting TypeError: Cannot read properties of undefined (reading 'findAll') error and I couldn't find why. Here is my index.js and User.js files in models folder;

index.js;

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require('../../config/database.js');

const db = {};

const sequelize = new Sequelize(config);

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.hostname] = model;
    db[model.username] = model;
    db[model.password] = model;
    db[model.command] = model;
    db[model.status] = model;
    db[model.cpu] = model;
    db[model.mac] = model;
    db[model.info] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

User.js;

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    /* id : DataTypes.INTEGER, */
    hostname: DataTypes.STRING,
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    command: DataTypes.STRING,
    status: DataTypes.STRING,
    cpu: DataTypes.INTEGER,
    mac: DataTypes.STRING,
    info: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

And here is my controller UserController.js file;

const { User } = require('../models');

module.exports = {

    index(req, res) {
        User.findAll({})
            .then(users => res.json({
                error: false,
                data: users
            }))
            .catch(error => res.json({
                error:true,
                data: [],
                error: error
            }));
    },

    create(req, res) {
        const { /* id, */hostname,username,password,command,status,cpu,mac,info} = req.body;
        User.create({
            /* id, */hostname,username,password,command,status,cpu,mac,info
        })
        .then(user => res.status(201).json({
            error: false,
            data: user,
            message: "new user has been created"
        }))
        .catch(error => res.json({
            error:true,
            data: [],
            error: error
        }));
    },

    update(req, res) {
        const user_id = req.params.id;

        const { hostname,username,password,command,status,cpu,mac,info } = req.body;

        User.update({
            hostname,username,password,command,status,cpu,mac,info
        }, {
            where: {
                id: user_id
            }
        })
        .then(user => res.status(201).json({
            error: false,
            data: user,
            message: 'user has been updated'
        }))
        .catch(error => res.json({
            error: true,
            error: error
        }));
    },

    destroy(req, res) {
        const user_id = req.params.id;

        User.destroy({ where: {
            id: user_id
        }})
        .then(status => res.status(201).json({
            error: false,
            message: 'user has been deleted'
        }))
        .catch(error => res.json({
            error: true,
            error: error
        }));
    }
}

I couldn't figure out what caused this problem. I'd be glad if you could help.Here is my project directory;

directory

You're passing an empty object to User.findAll({}) in your UserController.js file.

Try User.findAll() , instead.

I had the same problem just last night, and it was happening to me because I was calling findAll() before I synced sequelize sequelize.sync() . That is why it is returning undefined. Not sure if it happening to you for the same reason because you haven't attached the code for your server.js. Also either pass in an attribute prop inside the findAll({}) or get rid of the curly braces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM