无法使用从 sequelize-auto 生成的模型查询数据库



我遵循了如何从现有数据库生成模型的说明https://github.com/sequelize/sequelize-auto。然而,当我尝试使用模型查询数据库时,我得到这个错误:

TypeError:不能将undefined或null转换为object在函数。键()在函数。findAll (/home/jefry/节点/sequelize/node_modules/sequelize/src/model.js: 1755:47)在/home/jefry/node/sequelize/src/components/users/controllers.ts:霎时一切都在发电机。next ()在/home/jefry/node/sequelize/src/components/users/controllers.ts: 31:71at new Promise ()在__awaiter (/home/jeffrey/node/sequelize/src/components/users/controllers.ts:27:12)At list (/home/jeffrey/node/sequelize/src/components/users/controllers.ts:45:12)在层。处理[as handle_request] (/home/jeffery/node/sequelize/node_modules/express/lib/router/layer.js:95:5)at next (/home/jeffri/node/sequelize/node_modules/express/lib/router/route.js:144:13)

控制器*

export async function list(req: Request, res: Response) {
try {
const list = await primera.findAll();
res.json({ list });
} catch (error) {
console.log(error);
}
}

*

import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
export interface primeraAttributes {
verdad?: number;
}
export type primeraOptionalAttributes = "verdad";
export type primeraCreationAttributes = Optional<primeraAttributes, primeraOptionalAttributes>;
export class primera extends Model<primeraAttributes, primeraCreationAttributes> implements primeraAttributes {
verdad?: number;

static initModel(sequelize: Sequelize.Sequelize): typeof primera {
return primera.init({
verdad: {
type: DataTypes.BOOLEAN,
allowNull: true
}
}, {
sequelize,
tableName: 'primera',
timestamps: false
});
}
}

init-models

import type { Sequelize } from "sequelize";
import { primera as _primera } from "./primera";
import type { primeraAttributes, primeraCreationAttributes } from "./primera";
export {
_primera as primera,
};
export type {
primeraAttributes,
primeraCreationAttributes,
};
export function initModels(sequelize: Sequelize) {
const primera = _primera.initModel(sequelize);

return {
primera: primera,
};
}

您应该至少传递一个空对象作为findAll的参数:

const list = await primera.findAll({});

相关内容

  • 没有找到相关文章

最新更新