I am trying to get list from my mongodb by passing a condition but anytime I print my condition, I get undefined.
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 };
}
This is my repo
public async listAll() {
const conditions = { role: 'surf' };
const data = { condition: conditions, page: 1, limit: 10 };
const all = await this.list(data);
return all
}
This does not take the condition into account and just returns all the users.
My Pagination query interface looks like this
export interface PaginationQuery {
archived?: boolean | string;
conditions?: any;
page?: number;
limit?: number;
projections?: any;
sort?: string | object;
}