Sequelize Object is possible undefined

48 views Asked by At

I am trying to read a read only DB on a server.

I was able to generate the models in TS files using the command

sequelize-auto -o "./models" -d myDB -h webHostAddress -x Pwd -u root -p 3306 -v --sg -e mysql --cf p --cp c --cm p -l ts

However for the below snippet, I am getting the error Object is possibly 'undefined'. How do I resolve this?


import { initModels, Flag } from "./models/init-models";

// import models into sequelize instance
initModels(this.sequelize); // Object is possibly 'undefined'.

const myFlags = await Flag.findAll({});

I was expecting the connection to read only DB and do findAll(). My goal is to do an equivalent of Select * from flags

I had to resort to the above because I couldn't do a select all using this https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#simple-select-queries

I had to specify the attributes and only then the query worked.

Model.findAll({
  attributes: ['foo', 'bar']
});
0

There are 0 answers