felixmosh / knex-paginate

An extension of Knex's query builder with `paginate` method that will help with your pagination tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't extend QueryBuilder with existing method ('paginate').

MoreiraTv opened this issue · comments

I'm getting this error when starting my code:

Error: Can't extend QueryBuilder with existing method('paginate').
at Function.Builder.extend (/apps/api-cantinho-da-limpeza/node_modules/knex/lib/query/querybuilder.js:1756:11)
at Object.extend (/apps/api-cantinho-da-limpeza/node_modules/knex/lib/knex-builder/Knex.js:30:18)
at attachPaginate (/apps/api-cantinho-da-limpeza/node_modules/knex-paginate/lib/index.js:78:21)
at materials (/apps/api-cantinho-da-limpeza/src/routes/materials.ts:7:1)
at Object. (/apps/api-cantinho-da-limpeza/src/routes/materials.ts:9:23)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Object.F (/apps/api-cantinho-da-limpeza/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:941)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)

I made the imports and stationed in the way specified in the documentation
``
import { FastifyInstance } from "fastify";
import { z } from "zod";
import { knex } from "../database"
import crypto from "node:crypto";
import { attachPaginate } from 'knex-paginate'

attachPaginate();

``

I'm using these dependencies:
"dependencies": {
"dotenv": "^16.0.3",
"fastify": "^4.14.1",
"knex": "^2.4.2",
"knex-paginate": "^3.1.0",
"pg": "^8.10.0",
"zod": "^3.21.4"
},

Hi @MoreiraTv , this error happens if you are trying to extend the query builder with something that already exists.

You need to call to attachPagination only once in a project (usually in the place you configure your db connection)
If you don't know where it comes from... You can wrap each usage of attachPagination with an if that checks if paginate doesn't exists, add it otherwise do nothing.

I had the same issue with in my test suite, in my case I've moved the call to attachPaginate to tests setup

Thanks for the horientation I made the change to my connection configuration file, really I was calling in two places.