简体   繁体   English

类型错误:无法读取未定义的属性(读取“findAll”)

[英]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.我收到TypeError: Cannot read properties of undefined (reading 'findAll')错误,我找不到原因。 Here is my index.js and User.js files in models folder;这是models文件夹中的index.jsUser.js文件;

index.js;索引.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;用户.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;这是我的控制器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
        }));
    }
}

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.您将一个空对象传递给UserController.js文件中的User.findAll({})

Try User.findAll() , instead.尝试User.findAll() ,而不是。

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() .昨晚我遇到了同样的问题,它发生在我身上,因为我在同步 sequelize sequelize.sync() findAll() ) 。 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.不确定它是否出于同样的原因发生在您身上,因为您没有附加 server.js 的代码。 Also either pass in an attribute prop inside the findAll({}) or get rid of the curly braces.也可以在findAll({})中传递一个属性 prop 或去掉花括号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 sequelize 不识别 Model? 类型错误:无法读取未定义的属性(读取“findAll”) - Why does sequelize not recognize the Model? TypeError: Cannot read properties of undefined (reading 'findAll') Sequelize,Express JS - 无法读取未定义的属性(读取“findAll”) - Sequelize, Express JS - Cannot read properties of undefined (reading 'findAll') 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取“学生”) - TypeError: Cannot read properties of undefined (reading 'students') TypeError:无法读取未定义的属性(读取“addEventListener”) - TypeError: Cannot read properties of undefined (reading 'addEventListener') TypeError:无法读取未定义的属性(读取'indexOf') - TypeError: Cannot read properties of undefined (reading 'indexOf') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country') TypeError:无法读取未定义的属性(读取“然后”) - TypeError: Cannot read properties of undefined (reading 'then') TypeError:无法读取未定义的属性(读取“设置”) - TypeError: Cannot read properties of undefined (reading 'set')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM