Sequelize TypeError:无法将未定义或空转换为对象
2020-01-16
11003
我尝试将 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;
控制器
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);
},
};
2个回答
我也遇到了同样的情况,问题是我忘记在数据库索引中导入我的模型,所以也许可以尝试这样的方法:
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));
}
}
Ana Figueira
2020-01-21
只是为了让大家知道发生在我身上的另一件事让我返回了同样的错误。
这是我的存储在 /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;
}
这是我的 db init 函数,如您所见,它正确指向我的模型类:
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');
});
}
这是我调用 list() 函数时给我错误的服务:
import { DefaultContentView } from "/models/core-data/online-site/default-Content-view";
export class DefaultContentViewService {
static list(): Promise<DefaultContentView[] | null> {
return DefaultContentView.findAll();
}
}
一切似乎都正常,甚至没有一个错误,但是当调用该函数时,它不断给我返回错误。我花了大约 3 个小时进行调试,然后我发现服务类中的模型导入中有一个错误的大写“C”(可能是由于替换造成的),我将“/models/core-data/online-site/ default-Content-view ”更改为“/models/core-data/online-site/ default-content-view ”,然后一切都开始按预期工作。
我认为这是因为即使导入在 nodejs 中不区分大小写,Sequelize 也会以某种方式处理它。
我希望这有助于节省调试此类问题的时间。
mattia ferraro
2023-03-13