无法读取未定义的属性“orderBy”
2022-01-03
893
我想按 adViews 的长度对数组进行排序。
但是我收到以下错误:
TypeError: Cannot read property 'orderBy' of undefined
如何修复它?
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),
},
};
}
2个回答
从外观上看,Lodash 不使用默认导出。使用
import * as _ from 'lodash';
应该没问题。
Jay McDoniel
2022-01-03
看来你的 lodash 模块没有被导入。 尝试只导入你需要的函数
import { orderBy } from 'lodash';
Smriti Shikha
2022-01-03