如何将排序参数传递给订单?

How to pass a sort parameter to order?

我想让它可以选择按什么值排序。

不能这样传递参数:

 order: {
     sort: order
 },

我该怎么做?

const search = query.search || '';
const order = query.order || 'ASC';
const sort = query.sort || '';

const [data, total] = await this.userRepository.findAndCount({
    relations: ['role'],
    where: [{
        username: ILike(`%${search}%`),
        email: ILike(`%${search}%`)
    }],
    order: {
        sort: order
    },
    take,
    skip: (page - 1) * take
});

您要找的是这个:

const [data, total] = await this.userRepository.findAndCount({
    ...
    order: {
        [sort]: order
    },
    ...
});

尽管如此,我应该警告您,当 sort.

的字符串为空时,这可能会抛出异常