gpt4 book ai didi

node.js - 聚合函数返回 null GraphQL

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:30 25 4
gpt4 key购买 nike

我正在使用 Sequelize 中的计数来测试基本聚合函数,这是我的类型计数:

type Creserve {
id: ID!
rDateStart: Date!
rDateEnd: Date!
grade: Int!
section: String!
currentStatus: String!
user: User!
cartlab: Cartlab!
}
type Counts {
section: String!
count: Int
}
type Query {
getBooking(id: ID!): Creserve!
allBookings: [Creserve]
getBookingByUser(userId: ID): Creserve
upcomingBookings: [Creserve]
countBookings: [Counts]
}

我使用 countBookings 作为聚合函数的查询,这是我的查询解析器:

countBookings: async (parent, args, {models}) => 
{
const res = await models.Creserve.findAndCountAll({
group: 'section',
attributes: ['section', [Sequelize.fn('COUNT', 'section'), 'count']]
});
return res.rows;
},

它输出的查询是这样的:

Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";

并在我的 psql shell 中尝试了这个查询,它工作正常:

 section | count
---------+-------
A | 2
R | 2

但是,当我尝试在 GraphQL Playground 中查询 countBookings 时,返回了部分,但没有返回计数:

    {
"data": {
"countBookings": [
{
"section": "A",
"count": null
},
{
"section": "R",
"count": null
}
]
}
}

我是不是漏掉了什么?或者这是一个错误?这是我在这个例子中尝试遵循的答案:https://stackoverflow.com/a/45586121/9760036

非常感谢!

编辑:返回 console.log(res.rows) 输出如下所示:

   [ Creserve {
dataValues: { section: 'A', count: '2' },
_previousDataValues: { section: 'A', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: [Object],
omitNull: false,
hooks: [Object],
sequelize: [Sequelize],
uniqueKeys: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array] },
__eagerlyLoadedAssociations: [],
isNewRecord: false },
Creserve {
dataValues: { section: 'R', count: '2' },
_previousDataValues: { section: 'R', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: [Object],
omitNull: false,
hooks: [Object],
sequelize: [Sequelize],
uniqueKeys: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array] },
__eagerlyLoadedAssociations: [],
isNewRecord: false } ]

这是 res.count:

Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";
[ { count: '2' }, { count: '2' } ]

最佳答案

问题

实际上,您正在此处执行所有操作...但是这里发生的情况是 sequlize 不返回普通对象...它始终以 instance< 形式返回数据 像这样

[ Creserve {
dataValues: { section: 'A', count: '2' },
_previousDataValues: { section: 'A', count: '2' },
_changed: {},
_modelOptions:
{ timestamps: true,

解决方案

I am not sure but there is no other way instead of looping and makes response to json object...

const array = []
res.rows.map((data) => {
array.push(data.toJSON())
})
return array

关于node.js - 聚合函数返回 null GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com