Sequelize,Typescript 访问原始查询的结果
Sequelize, Typescript accessing results of a raw query
我正在使用 Sequlize 和 Typescript。必须执行一些原始查询才能从 PostgreSQL 数据库中获取数据(现有类别的所有父类别)。作为对象数组。基本上,递归 SELECT 语句。
根据 Sequelize 手册,原始查询 return 有两个参数,result
和 metadata
(通常是一些受影响的行),除非您明确指定查询类型。在这种情况下,它应该 return 只是结果。
所以最终我的代码如下所示:
const parentCat = await SomeDataModel.sequelize?.query(
getAllParentCategories,
{
replacements: { subcategory_code: ARRAY_OF_CATEGORIES_TO_SEARCH },
type: QueryTypes.SELECT
}
);
根据分配的类型,returns parentCat
[undefined, number] 的类型 |未定义`。假设数组中的第一个参数是结果。
如果我尝试 console.log 结果它 return 是一个对象数组(如预期的那样):
[
{ category_code: 'Value1' },
{ category_code: 'Value2' },
{ category_code: 'Value3' },
{ category_code: 'Value4' },
{ category_code: 'Value5' }
]
但是,如果我尝试解构这个数组并使用它,就会一直抱怨该对象可能未定义。即使我检查数组存在,元素也不是undefined或null,并且数组的长度大于0)
然后当我尝试访问对象的 属性(例如:category_code)时,它一直抱怨 Property 'category_code' does not exist on type 'number'
。我的代码是:
if (parentCat !== undefined && parentCat.length > 0) {
const additionalCats = parentCat.reduce<string[]>((acc, currentCategory) => {
return currentCategory ? [...acc, currentCategory.category_code] : acc;
}, [])
console.log(additionalCats);
}
此外,这里的三元运算无论如何都是多余的,导致 DB 要么 return 是一个空数组,要么是其中包含类别的对象数组。因为我已经检查过数组是否为空,所以我认为这没有多大意义。但是如果我试图摆脱它,Typescript 会一直抱怨。
也许有人可以提出任何解决此问题的建议?
抱歉我的英语不好,来自墨西哥的问候,我认为这将是您的解决方案。
我正在尝试做一个 crud.find(p => p.Permission === "Update")?.Permission;
但错误提示:属性 在类型 'object' 上不存在。ts(2339)
我在这里找到了解决方案。
Sequelize: mapping between raw data and model
他说:您可以在查询中设置选项 mapToModel: true 以指示 Sequelize 将返回的字段名称转换为您提供的模型中的等效名称。
`const User = sequelize.define('user', {
userId: {
field: 'user_id',
type: DataTypes.BIGINT,
primaryKey: true
})
sequelize.query(
'select user_id from user where user_id > 1000',
{
mapToModel: true,
model: User
}
)`
我正在使用 Sequlize 和 Typescript。必须执行一些原始查询才能从 PostgreSQL 数据库中获取数据(现有类别的所有父类别)。作为对象数组。基本上,递归 SELECT 语句。
根据 Sequelize 手册,原始查询 return 有两个参数,result
和 metadata
(通常是一些受影响的行),除非您明确指定查询类型。在这种情况下,它应该 return 只是结果。
所以最终我的代码如下所示:
const parentCat = await SomeDataModel.sequelize?.query(
getAllParentCategories,
{
replacements: { subcategory_code: ARRAY_OF_CATEGORIES_TO_SEARCH },
type: QueryTypes.SELECT
}
);
根据分配的类型,returns parentCat
[undefined, number] 的类型 |未定义`。假设数组中的第一个参数是结果。
如果我尝试 console.log 结果它 return 是一个对象数组(如预期的那样):
[
{ category_code: 'Value1' },
{ category_code: 'Value2' },
{ category_code: 'Value3' },
{ category_code: 'Value4' },
{ category_code: 'Value5' }
]
但是,如果我尝试解构这个数组并使用它,就会一直抱怨该对象可能未定义。即使我检查数组存在,元素也不是undefined或null,并且数组的长度大于0)
然后当我尝试访问对象的 属性(例如:category_code)时,它一直抱怨 Property 'category_code' does not exist on type 'number'
。我的代码是:
if (parentCat !== undefined && parentCat.length > 0) {
const additionalCats = parentCat.reduce<string[]>((acc, currentCategory) => {
return currentCategory ? [...acc, currentCategory.category_code] : acc;
}, [])
console.log(additionalCats);
}
此外,这里的三元运算无论如何都是多余的,导致 DB 要么 return 是一个空数组,要么是其中包含类别的对象数组。因为我已经检查过数组是否为空,所以我认为这没有多大意义。但是如果我试图摆脱它,Typescript 会一直抱怨。
也许有人可以提出任何解决此问题的建议?
抱歉我的英语不好,来自墨西哥的问候,我认为这将是您的解决方案。
我正在尝试做一个 crud.find(p => p.Permission === "Update")?.Permission;
但错误提示:属性 在类型 'object' 上不存在。ts(2339) 我在这里找到了解决方案。
Sequelize: mapping between raw data and model
他说:您可以在查询中设置选项 mapToModel: true 以指示 Sequelize 将返回的字段名称转换为您提供的模型中的等效名称。
`const User = sequelize.define('user', {
userId: {
field: 'user_id',
type: DataTypes.BIGINT,
primaryKey: true
})
sequelize.query(
'select user_id from user where user_id > 1000',
{
mapToModel: true,
model: User
}
)`