【问题标题】:objection.js modifyEager .as('count') doesn't seem to do anything to .count()objection.js modifyEager .as('count') 似乎对 .count() 没有任何作用
【发布时间】:2019-07-30 16:43:11
【问题描述】:

所以我想获取帖子中 cmets 的数量,而不是所有 cmets 本身 我已经让它像这样与 modifyEager 一起使用,但在这种情况下,as('count') 似乎什么也没做

.eager('comments').modifyEager("comments", builder => {
    builder.count().as('count')
})

这真的很乏味,因为每个帖子的 cmetsCount 都有这些值:

[]
[]
[]
[]
[ Post { 'count(*)': 1 } ]
[]

所以我得到了计数,但我肯定做错了什么

if(commentCount.length > 0)
    return data[0]["count(*)"]
return 0

有什么想法吗?

编辑:显示工作代码以供参考以帮助人们

module.exports = async (organizationId, scope) => {
    return await Post.query()
    .where("organization_id", organizationId)
    .orderBy("post_id", "desc")
    .limit(scope.limit)
    .offset(scope.offset)
    .select(
        'posts.*',
        Post.relatedQuery('comments').count().as('commentsCount')
    )
    .eager('users')
}

【问题讨论】:

    标签: node.js mysql2 eager objection.js


    【解决方案1】:

    无法对其进行测试,但该示例以这种方式出现在文档中:

    const posts = await Post
      .query()
      .select(
        'Post.*',
        Post.relatedQuery('comments').count().as('numberOfComments')
      );
    
    console.log(posts[4].numberOfComments);
    

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多