我正在做一个Electron 商务API,我有这个类用于我的搜索查询,我使用正则表达式和带有 node 的类型脚本来实现这一点,我使用js node 项目来建立我的基础,但当我使用"Localhost:4000/api/Products/?Page=1"时,过滤器返回为空:

export class APIFeatures {

    query: any;
    queryStr: any;

    constructor(query: any, queryStr: any) {
        this.query = query,
            this.queryStr = queryStr
    }

    search(): any {
        const keyword = this.queryStr.keyword ? {
            name: {
                $regex: this.queryStr.keyword,
                $options: 'i'
            }
        } : {}

        this.query = this.query.find({ ...keyword })


    }


    filter(): any {

        const queryCopy = { ...this.queryStr };

        // Removing fields from the query
        const removeFields = ['keyword', 'limit', 'page']
        removeFields.forEach(el => delete queryCopy[el]);

        // Advance filter for price, ratings etc
        let queryStr = JSON.stringify(queryCopy)
        queryStr = queryStr.replace(/\b(gt|gte|lt|lte)\b/g, match => `$${match}`)
        // console.log(queryStr)

        this.query = this.query.find(JSON.parse(queryStr));
        return this;
    }

    pagination(resPerPage: any): any {
        const currentPage = Number(this.queryStr.page) || 1;
        const skip = resPerPage * (currentPage - 1);

        this.query = this.query.limit(resPerPage).skip(skip);
        return this;
    }


}


这就是产品控制器,我想在其中执行serach、过滤器和分页,以个性化来自产品数据库的查询:

 async getAllProducts(req: Request, res: Response) {
        try {

            const resPerPage = 8;
            const productCount = await prisma.product.count();

            const apiFeatures = new APIFeatures(prisma.product.findMany(), req.query)
                .search()
                .filter()
                .pagination(resPerPage)

            const response = apiFeatures.query;

            return res.status(200).send({ success: true, response, 
   productCount })
        } catch (error) {
            res.send({
                message: error
            })
        }
    }

问题是为什么我的查询返回为空

推荐答案

只需像这样修复您的Search()函数,它应该会被修复

search(): any {
        const keyword = this.queryStr.keyword ? {
            name: {
                $regex: this.queryStr.keyword,
                $options: 'i'
            }
        } : {}

        this.query = this.query.find({ ...keyword });

        return this;
    }

Node.js相关问答推荐

我的位置也移动时左右拖动谷歌 map

Mongoose抱怨说,整数是数字,而不是整数

关于Node.js中的AES加密库的问题

使用参考中断Mongoose模型-Node.js

我收到警告:发现函数rs-ms-v1不受支持的运行时nodejs18.x× 不受支持的运行时

TS[2339]:类型 '() => Promise<(Document & Omit) | 上不存在属性空>'

错误 node :错误:绑定消息提供 16 个参数,但准备语句需要 15 个

错误:无法为 /blog 收集页面数据并且在 object.fetch 处获取失败

在数组的另一个对象中获取数组的mongoose 对象

Web3.js 脚本在监听 CreatedPairs 时退出

在 ExpressJS 中将变量传递给 JavaScript

在 PassportJS 中使用多种本地策略

如何在 AWS 上的 Amazon Linux AMI 中自动启动 node.js 应用程序?

Node.JS 中的基本 HTTP 身份验证?

node.js(ES6 / Babel)中 import X 和 import * as X 的区别?

User.findOrCreate 函数是做什么的,什么时候在Passport 中调用它?

Node.js, require.main === 模块

Express.js:没有这样的文件或目录

如何在 NodeJS 中拆分和修改字符串?

Google Firebase 错误(函数返回未定义、预期的 Promise 或值)