【问题标题】:TypeError: Cannot read properties of undefined (reading 'findAll')类型错误:无法读取未定义的属性(读取 \'findAll\')
【发布时间】:2022-12-24 14:20:57
【问题描述】:

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

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

【问题讨论】:

  • 看起来你错过了使用 sequelize.sync() 将你的模型与数据库同步

标签: javascript sqlite


【解决方案1】:

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

请试试User.findAll(),而不是。

【讨论】:

  • @Didem,这有帮助吗?你能在这个问题上取得任何进展吗?
【解决方案2】:

就在昨晚我遇到了同样的问题,它发生在我身上是因为我在同步 sequelize 之前调用了 findAll()。不确定它是否出于同样的原因发生在您身上,因为您没有附加 server.js 的代码。也可以在 findAll({}) 中传入属性 prop 或去掉花括号。

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 2022-12-08
    • 2022-12-01
    • 2022-12-21
    • 2022-12-25
    • 2023-01-21
    • 2023-02-06
    • 1970-01-01
    • 2021-12-03
    相关资源
    最近更新 更多