【问题标题】:Cannot read property 'orderBy' of undefined无法读取未定义的属性“orderBy”
【发布时间】:2022-01-03 15:08:25
【问题描述】:

我想按 adViews 的长度对数组进行排序。

但我收到以下错误:

TypeError: 无法读取未定义的属性“orderBy”

我该如何解决?

  import _ from 'lodash';

  async maxViewsQuery(page = 1, relations = []): Promise<PaginatedResult> {
    const take = 14;
    const [data, total] = await this.adRepository.findAndCount({
      where: `("Ad__adViews"."id" IS NOT NULL)`,
      take,
      skip: (page - 1) * take,
      relations,
    });

    const sort = _.orderBy(data, (view) => view.adViews.length);

    return {
      data: sort.map((ad: Ad) => ({
        id: ad.id,
        title: ad.title,
        adViews: ad.adViews.length,
      })),
      meta: {
        total,
        page,
        lastPage: Math.ceil(total / take),
      },
    };
  }

【问题讨论】:

    标签: node.js lodash nestjs


    【解决方案1】:

    从外观上看,Lodash 不使用默认导出。使用

    import * as _ from 'lodash';
    

    应该没问题的。

    【讨论】:

      【解决方案2】:

      您的 lodash 模块似乎没有被导入。 尝试只导入您需要的功能

      import { orderBy } from 'lodash';

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-06
        • 2020-10-30
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多