gpt4 book ai didi

javascript - Sequelize TypeError : Cannot convert undefined or null to object

转载 作者:行者123 更新时间:2023-12-03 12:16:31 31 4
gpt4 key购买 nike

我正在尝试将 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);
},
};

最佳答案

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

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));
}
}

关于javascript - Sequelize TypeError : Cannot convert undefined or null to object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776498/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com