我有一个posts
解析器,它使用这种逻辑来返回帖子:
const qb = Post.createQueryBuilder('post')
.select(['post.id AS id', 'post.createdAt AS id'])
.addSelect(
"json_build_object('id', user.id, 'username', user.username, 'email', user.email, 'createdAt', user.createdAt, 'updatedAt', user.updatedAt)",
'creator'
)
if (req.session.userId)
qb.addSelect((qb) => {
return qb
.subQuery()
.select('updoot.value')
.from(Updoot, 'updoot')
.where('"userId" = :userId AND "postId" = post.id', {
userId: req.session.userId
})
}, 'voteStatus')
else qb.addSelect('null', 'voteStatus')
qb.innerJoin('post.creator', 'user')
.orderBy('post.createdAt', 'DESC')
.take(realLimitPlusOne)
if (cursor)
qb.where('post.createdAt < :cursor', {
cursor: new Date(parseInt(cursor))
})
qb.printSql()
const posts = await qb.getRawAndEntities()
console.log('posts: ', posts['raw'][0])
return {
posts: posts['raw'].slice(0, realLimit),
hasMore: posts['raw'].length === realLimitPlusOne
}
我希望我的数据以这种方式结构化:
posts: {
id: 29,
createdAt: 2023-02-07T06:53:39.453Z,
creator: {
id: 44,
username: 'prashant',
email: '[email protected]',
createdAt: '2023-02-06T13:02:11.717504',
updatedAt: '2023-02-06T13:02:11.717504'
},
voteStatus: -1
}
但是我得到的不是id
和createdAt
,而是post_id
和post_createdAt
,这与我的graphql类型不兼容。即使我为这些列添加了别名,我也得到了:
posts: {
post_id: 29,
post_createdAt: 2023-02-07T06:53:39.453Z,
creator: {
id: 44,
username: 'prashant',
email: '[email protected]',
createdAt: '2023-02-06T13:02:11.717504',
updatedAt: '2023-02-06T13:02:11.717504'
},
voteStatus: -1
}
我的代码是否有问题,或者我是否应该创建一个Map函数,将键名Map到适当的键名?
1条答案
按热度按时间tez616oj1#
我也有一个“列'distinctly._id'不存在”的错误。这是因为我没有在查询中选择id。所以这并不是说键名不兼容,而是它必须始终选择一个项目的ID,而你没有选择它。