【发布时间】: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),
},
};
}
【问题讨论】: