javascript - 传递条件 Mongoose NodeJS
问题描述
我试图通过传递一个条件从我的 mongodb 中获取列表,但是每当我打印我的条件时,我都会得到未定义的。
async list(query: PaginationQuery): Promise<any> {
const page = Number(query.page) - 1 || 0;
let limit = Number(query.limit) || 20;
const offset = page * limit;
const sort = query.sort || 'createdAt';
const archived = this.convertArchived(query.archived);
const conditions = {
...query.conditions,
...(!archived
? { deletedAt: undefined }
: { deletedAt: { $ne: undefined } })
};
console.log(`HELP MEEE xx ${JSON.stringify(query.conditions)}`)
const data = await this.model
.find(conditions)
.select(query.projections)
.skip(offset)
.limit(limit)
.sort(sort);
const totalDocuments = await this.model.countDocuments(conditions);
const pageCount = Math.ceil(totalDocuments / limit);
const pagination = {
page: page + 1,
limit,
sortedBy: sort,
pageCount
};
return { data, pagination };
}
这是我的回购
public async listAll() {
const conditions = { role: 'surf' };
const data = { condition: conditions, page: 1, limit: 10 };
const all = await this.list(data);
return all
}
这不考虑条件,只返回所有用户。
我的分页查询界面是这样的
export interface PaginationQuery {
archived?: boolean | string;
conditions?: any;
page?: number;
limit?: number;
projections?: any;
sort?: string | object;
}
解决方案
推荐阅读
- python - 计算多边形中的点并将结果写入 (Geo)Dataframe
- measurement - 使用压缩传感的荧光光谱测量?
- kubernetes - 如何在 Homelab 中将共享驱动器用作多个 Kubernetes PV
- abap - SAP ABAP CDS 查看/DCL 检查授权
- mysql - 当文本未正确存储并存储在其他字符集中时如何转换整个 MySQL 数据库
- ios - UIScrollView 上的跳跃键盘被解雇
- spring - 如何在春季启动中使用条件接口与列表中的值
- wifi - ISP 是否注意到对您的 wifi 网络/路由器的暴力攻击?
- php - WSL2 Ubuntu 20.04、Docker 和 Laravel 启动本地主机不会显示在浏览器上
- blazor - 谁能看到为什么我的用户没有通过身份验证访问主页?