首页 > 解决方案 > 传递条件 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;
}

标签: javascriptnode.jstypescriptmongodbmongoose

解决方案


推荐阅读