首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:无法读取未定义的属性(读取“findAll”)

TypeError:无法读取未定义的属性(读取“findAll”)
EN

Stack Overflow用户
提问于 2022-05-26 15:36:29
回答 1查看 2K关注 0票数 2

我得到了TypeError: Cannot read properties of undefined (reading 'findAll')错误,我找不到原因。这是我的index.jsUser.js文件在models文件夹中;

index.js;

代码语言:javascript
运行
复制
'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;

代码语言:javascript
运行
复制
'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文件;

代码语言:javascript
运行
复制
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
        }));
    }
}

我不知道是什么引起了这个问题。如果可以的话,我很高兴help.Here是我的项目目录;

目录

EN

回答 1

Stack Overflow用户

发布于 2022-05-26 16:08:36

您正在将一个空对象传递给User.findAll({})文件中的UserController.js文件。

试一试User.findAll()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72394394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档