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

使用Sequelize连接Oracle时出现Cannot read properties of undefined错误

Sequelize连接Oracle报错:Cannot read properties of undefined (reading 'length')

尝试用Sequelize连接Oracle数据库并获取数据时,出现错误:Cannot read properties of undefined (reading 'length'): undefined。相关代码如下:

连接代码

import { Sequelize, DataTypes } from "sequelize";
import _projects from "../models/projects.js";

export const getProjects = async (req, res, next) => {
var conn = new Sequelize({
            dialect: 'oracle',
            username: dbAdmin,
            password: dbPass,
            dialectOptions: { connectString: connStr } // 也试过{ connectionString: connStr }
        });

        function initModel(connection) {
            const projects = _projects.init(connection, DataTypes);
            return { projects };
        }

        var db = initModel(conn);
        const all_projects = await db.projects.findAll()
        console.log("all_projects", all_projects.rows); // 报错位置:Cannot read properties of undefined (reading 'length'): undefined
}

projects.js

import _sequelize from 'sequelize';
const { Model, Sequelize } = _sequelize;

export default class projects extends Model {
    static init(sequelize, DataTypes) {
        return super.init({
            id: {
                type: DataTypes.UUID,
                allowNull: false,   
                primaryKey: true
            },
            credat: {
                type: DataTypes.DATE,
                allowNull: true,
                defaultValue: Sequelize.fn('getdate')
            },
            description: {
                type: DataTypes.STRING(1024),
                allowNull: true
            },
            picture: {
                type: DataTypes.STRING(255),
                allowNull: true
            },
            settings: {
                type: DataTypes.TEXT,
                allowNull: true
            },
            is_deleted: {
                type: DataTypes.INTEGER,
                allowNull: true
            }
        }, {
            sequelize,
            tableName: 'projects',
            schema: 'dbo',
            timestamps: false,
        });
    }
}

问题原因及修复方案

  1. 核心错误点:findAll()返回的是直接的结果数组,而非带有rows属性的对象。你尝试访问all_projects.rows,但数组不存在该属性,进而触发读取length的未定义错误。
  2. 基础修复:直接使用all_projects本身即可获取查询结果:
    console.log("all_projects", all_projects);
    
  3. 额外优化建议:
    • 添加连接测试和错误捕获,避免隐性问题:
      export const getProjects = async (req, res, next) => {
        try {
          var conn = new Sequelize({
            dialect: 'oracle',
            username: dbAdmin,
            password: dbPass,
            dialectOptions: { connectString: connStr }
          });
          // 验证数据库连接
          await conn.authenticate();
          console.log('数据库连接成功');
      
          function initModel(connection) {
            const projects = _projects.init(connection, DataTypes);
            return { projects };
          }
      
          var db = initModel(conn);
          const all_projects = await db.projects.findAll();
          console.log("all_projects", all_projects);
          res.json(all_projects);
        } catch (error) {
          console.error('数据库操作失败:', error);
          next(error);
        }
      }
      
    • 检查依赖:确保安装了oracledb(Sequelize 6+连接Oracle需要此依赖);
    • 验证schema:Oracle中默认schema通常是用户名而非dbo,确认schema: 'dbo'是否与实际数据库配置匹配;
    • 确认连接字符串格式:Oracle连接字符串一般为主机:端口/服务名,确保connStr格式正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:15:29