I'm trying to use sequelize model with an existing database in SQL Server. When I try to retrieve data from a table using raw query it works. But when I use a model I receive the follow error:
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)
Model:
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);
},
};
It happened the same to me and the problem was that I forgot to import my model in the database index, so maybe try something like this:
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));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With