Reputation: 101
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);
},
};
Upvotes: 10
Views: 11755
Reputation: 11
Just to let everyone know happend to me a different thing which was giving me back the same error.
This is my model class stored in /model/core-data/online-site/default-content-view.ts:
import { Model } from "sequelize";
export class DefaultContentView extends Model {
ViewModelId: number | undefined;
ContentTypeId: string | undefined;
ViewId: number | undefined;
}
This is my db init function which as you can see was correctly pointing to my model class:
import { DefaultContentView } from "/models/core-data/online-site/default-content-view";
// Gets database instance
export async function initDb(database: string, user: string, password: string, host: string, port: number) : Promise<void> {
// Gets connection properties from configuration
const coreDb = new Sequelize(
database,
user,
password,
{
host: host,
port: port,
dialect: 'mysql',
query:{
raw:true // this parameter makes sequelize to return only the model itself without the informations related to the instance
}
}
);
return coreDb.authenticate().then( () => {
console.log('Connection estabished to Db');
DefaultContentView.init({
ViewModelId: {
allowNull: false,
primaryKey: true,
type: DataTypes.INTEGER,
field: 'view_model_id'
},
ContentTypeId: {
allowNull: false,
primaryKey: true,
type: DataTypes.STRING(250),
field: 'content_type_id'
},
ViewId: {
allowNull: false,
type: DataTypes.INTEGER,
field: 'view_id'
}
}, {
timestamps: false,
tableName: 'os_default_content_view',
sequelize: coreDb
});
}).catch( () => {
console.log('Unable to connect to Db');
});
}
This is the service which was giving me the error when i was calling the list() function:
import { DefaultContentView } from "/models/core-data/online-site/default-Content-view";
export class DefaultContentViewService {
static list(): Promise<DefaultContentView[] | null> {
return DefaultContentView.findAll();
}
}
Everything seems to be ok, not even one error but when calling the function it was continuously giving me back the error. I spent like 3 hours debugging and i figured out there was a wrong upper case 'C' in the model import in the service class (probably due to a replace), I changed "/models/core-data/online-site/default-Content-view" to "/models/core-data/online-site/default-content-view" and everything started working as expected.
I think this is because even if import is case insensitive in nodejs Sequelize takes care of it in a certain way.
I hope this helps saving time in debugging this kind of issues.
Upvotes: 1
Reputation: 261
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));
}
}
Upvotes: 26