You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sequelize项目调用findALL报错:函数不存在的问题求助

问题排查:TypeError: hygroData.findALL is not a function

核心错误原因

  1. 方法名拼写错误:Sequelize中查询全量数据的方法是findAll()(最后一个L为小写),代码中误写为findALL()(最后两个L均为大写),这是触发报错的直接原因。
  2. Sequelize版本不兼容的关联定义:当前使用Sequelize v6,但模型仍采用已废弃的classMethods定义关联,会导致模型初始化异常,间接影响模型方法的可用性。

分步解决方案

1. 修正方法名拼写

修改routes/hygro.js中的方法调用,将findALL改为findAll:

module.exports = app => {
    const hygroData = app.db.models.Readings;
    app.get("/hygro", (req,res) => {
        hygroData.findAll({}).then(data => { // 修正方法名拼写
            res.json({data:data});
        })
    });
}

2. 适配Sequelize v6的关联定义方式

Sequelize v6已移除classMethods选项,需改用静态方法定义关联,同时修正db.js中的关联调用逻辑:

第一步:更新模型文件的关联定义

  • models/locations.js:
module.exports = (sequelize, DataType) => {
    const Locations = sequelize.define("Locations", {
        id: {
            type: DataType.INTEGER ,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataType.TEXT,
            unique: true,
            allowNull: false,
            validate: {
                notEmpty: true
        }
         },
         street: {
            type: DataType.TEXT,
            allowNull: false,
            validate : {
                notEmpty: true
            }

         },
         city: {
            type: DataType.TEXT,
            allowNull: false,
            validate:{
                notEmpty: true
            }
         },
         state: {
            type: DataType.TEXT,
            allowNull: false,
            validate:{
                notEmpty: true
            }
         },
         zip: {
            type: DataType.TEXT,
            allowNull: false,
            validate:{
                notEmpty: true
            }
         } 
    });

    // 改用静态方法定义关联
    Locations.associate = (models) => {
        Locations.hasMany(models.Readings);
    };

    return Locations;
}
  • models/readings.js:
module.exports = (sequelize, DataType) => {
    const Readings = sequelize.define("Readings",{
        id: {
            type: DataType.INTEGER ,
            primaryKey: true,
            autoIncrement: true
        },
        date: {
            type: DataType.TEXT,
            allowNull: false,
            validate: {
                notEmpty: true
        }
         },
         temperature: {
            type: DataType.INTEGER,
            allowNull: false,
            validate : {
                notEmpty: true
            }

         },
         humidity: {
            type: DataType.INTEGER,
            allowNull: false,
            validate:{
                notEmpty: true
            }
         } 
    });

    // 改用静态方法定义关联
    Readings.associate = (models) => {
        Readings.belongsTo(models.Locations);
    };

    return Readings;
}

第二步:修正db.js的关联调用逻辑

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");

let db = null;

module.exports = app => {
    if (!db){
        const config = app.libs.config;
        const sequelize = new Sequelize(
            config.database,
            config.username,
            config.password,
            config.params
        );
        db = {
            sequelize,
            Sequelize,
            models: {}
        };
        const dir = path.join(__dirname,"models");
        fs.readdirSync(dir).forEach(file => {
            const modelDir = path.join(dir, file);
            const model = require(modelDir)(sequelize,Sequelize.DataTypes);
            db.models[model.name]= model;
        });
        // 修正关联调用:检查并调用模型上的associate静态方法
        Object.keys(db.models).forEach(key => {
            if (db.models[key].associate) {
                db.models[key].associate(db.models);
            }
        });
    }
    return db;
};

3. 验证修复效果

重启Node.js服务后,访问http://localhost:3000/hygro即可正常获取数据。若数据库表未创建,可在libs/boot.js中添加模型同步逻辑:

module.exports = app => {
    app.db.sequelize.sync().then(() => {
        console.log("数据库模型同步完成");
        app.listen(app.get("port"), () => {
            console.log(`服务器运行在端口 ${app.get("port")}`);
        });
    });
};

内容的提问来源于stack exchange,提问作者Eddy Gallegos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:20:54