Sequelize TypeError:无法将未定义或null转换为对象

问题描述 投票:0回答:1

我正在尝试将sequelize模型与SQL Server中的现有数据库一起使用。当我尝试使用原始查询从表中检索数据时,它将起作用。但是,当我使用模型时,会收到以下错误:

Error Occurred TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Function.findAll (D:\Users\carlosli\Desktop\SigaWeb\node_modules\sequelize\lib\model.js:1692:47)
    at index (D:\Users\carlosli\Desktop\SigaWeb\src\controllers\consulta_controller.js:7:39)
    at Layer.handle [as handle_request] (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\layer.js:95:5)
    at D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:275:10)
    at jsonParser (D:\Users\carlosli\Desktop\SigaWeb\node_modules\body-parser\lib\types\json.js:110:7)
    at Layer.handle [as handle_request] (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:317:13)
    at D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Users\carlosli\Desktop\SigaWeb\node_modules\express\lib\router\index.js:335:12)

型号:

const { Model, DataTypes } = require('sequelize');

class Consulta extends Model {
    static init(sequelize) {
        super.init({
            seqConsulta: {
                type: DataTypes.INTEGER,
                allowNull: false,
                primaryKey: true,
                comment: "null",
                field: 'SEQ_CONSULTA'
            },
            descNickUsuario: {
                type: DataTypes.STRING(30),
                allowNull: false,
                comment: "null",
                field: 'DESC_NICK_USUARIO'
            },
            memConsulta: {
                type: DataTypes.STRING(2048),
                allowNull: true,
                comment: "null",
                field: 'MEM_CONSULTA'
            },
            descConsulta: {
                type: DataTypes.STRING(128),
                allowNull: true,
                comment: "null",
                field: 'DESC_CONSULTA'
            }
        }, {
            sequelize,
            modelName: 'Consulta',
            tableName: 'CONSULTAS',
            freezeTableName: true
        })
    }
};
module.exports = Consulta;

Controller

const Consulta = require('../models/CONSULTAS');

module.exports = {
    async index(req, res) {
        let consulta
        try {
            consulta = await Consulta.findAll({
                where: {
                    seqConsulta: 1
                },
            });
        } catch (e) {
            console.log("Error Occurred", e)
        }

        return res.json(consulta);
    },
};
javascript node.js sql-server database sequelize.js
1个回答
0
投票

我也发生了同样的事情,问题是我忘记了在数据库索引中导入我的模型,所以也许尝试这样的事情:

const Consulta = require('../app/models/Consulta ');
const models = [ Consulta ];
class Database {
  constructor() {
    this.init();
  }

  init() {
    this.connection = new Sequelize(DataBaseConfig);

    models.map(model => model.init(this.connection));
}
}
推荐问答
热门问答
最新问答