javascript TypeError:无法读取未定义的属性(正在阅读“findAll”)

ih99xse1  于 2022-12-25  发布在  Java
关注(0)|答案(2)|浏览(176)

我得到TypeError: Cannot read properties of undefined (reading 'findAll')错误,我找不到原因。这里是我的index.jsUser.js文件在models文件夹;
索引. 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;

用户. 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;
};

这是我的控制器UserController.js文件;

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
        }));
    }
}

我不知道是什么原因导致了这个问题。如果你能帮助我,我会很高兴。这是我的项目目录;
directory

im9ewurl

im9ewurl1#

您在UserController.js文件中向User.findAll({})传递了一个空对象。
请尝试User.findAll()

bihw5rsg

bihw5rsg2#

就在昨天晚上,我遇到了同样的问题,这发生在我身上,因为我在同步sequelize sequelize.sync()之前调用了findAll()。这就是为什么它返回undefined的原因。不确定它是否因为相同的原因发生在你身上,因为你没有附加server.js的代码。另外,要么在findAll({})中传递一个属性prop,要么去掉大括号。

相关问题