Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unknown type in cursor: [object] #16

Closed
mividtim opened this issue Nov 23, 2020 · 20 comments · Fixed by #24
Closed

unknown type in cursor: [object] #16

mividtim opened this issue Nov 23, 2020 · 20 comments · Fixed by #24
Labels
bug Something isn't working

Comments

@mividtim
Copy link

Hi there! This library is awesome. It's working great for most use cases I've encountered. However, I'm getting a weird error from the internals of the library occasionally when querying against test fixtures that were inserted into my local database by other tests prior to my test running. The column on which I'm trying to paginate has the PG type timestamp.

The entity is defined as follows:

    @Column({
        type: 'timestamp',
        nullable: true,
    })
    videoDisplayTime!: Date | null;

Here's the error coming out of the test run, with internals snipped out.

    Error: unknown type in cursor: [object]Mon Nov 23 2020 18:23:15 GMT+0000 (Coordinated Universal Time),at Object.encodeByType ([snip]/node_modules/typeorm-cursor-pagination/src/utils.ts:23:13),at payload.paginationKeys.map ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:178:21),at Array.map (<anonymous>),at Paginator.encode ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:176:41),at Paginator.<anonymous> ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:92:35),at Generator.next (<anonymous>),at fulfilled ([snip]/node_modules/typeorm-cursor-pagination/lib/Paginator.js:5:58),at process._tickCallback (internal/process/next_tick.js:68:7)
    --
    body: {
      "status": 500,
      "errorName": "Error",
      "message": "unknown type in cursor: [object]Mon Nov 23 2020 18:23:15 GMT+0000 (Coordinated Universal Time)",
      "stack": [
        "at Object.encodeByType ([snip]/node_modules/typeorm-cursor-pagination/src/utils.ts:23:13)",
        "at payload.paginationKeys.map ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:178:21)",
        "at Array.map (<anonymous>)",
        "at Paginator.encode ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:176:41)",
        "at Paginator.<anonymous> ([snip]/node_modules/typeorm-cursor-pagination/src/Paginator.ts:92:35)",
        "at Generator.next (<anonymous>)",
        "at fulfilled ([snip]/node_modules/typeorm-cursor-pagination/lib/Paginator.js:5:58)",
        "at process._tickCallback (internal/process/next_tick.js:68:7)"
      ]
    }

Any ideas on what I we may be doing wrong would be greatly appreciated!

@benjamin658
Copy link
Owner

benjamin658 commented Nov 24, 2020

Hi @mividtim , thanks for reporting the issue.

The library use Reflect.getMetadata(design:type...) to evaluate the type of specific pagination keys before encode a cursor.

I have tried to reproduce an issue, however everything is working fine:

import "reflect-metadata";
import { Entity, Column } from 'typeorm';

@Entity()
class Demo {
  @Column({
    type: 'timestamp',
    nullable: true,
  })
  videoDisplayTime!: Date | null;
}

console.log(Reflect.getMetadata('design:type', Demo.prototype, 'videoDisplayTime').name.toLowerCase()); // date

Could you provide more information about your runtime environment (version of typescript, ts-node, etc...)?

@mividtim
Copy link
Author

Hi there. Thanks for getting back so quickly. We're running "typescript": "3.7.2", "typeorm": "0.2.20", "ts-node": "8.4.1", "typeorm-cursor-pagination": "0.4.1", and typeorm-naming-strategies": "1.1.0". We use SnakeNamingStrategy for our table and column names... Not that I think that's relevant. We run all of this in the context of NestJS 6.8.4.

@mividtim
Copy link
Author

NodeJS 10.15.3, yarn 1.16.0, and postgres 11.2.

@benjamin658
Copy link
Owner

I have tried the same version of these dependencies, and everything is working fine.

Could you share a piece of code that using this lib?

@mividtim
Copy link
Author

I set up a contrived example. I believe the issue is with the naming strategy. This pagination library doesn't seem to use the naming strategy set in the TypeOrm configuration to derive proper column names for its pagination keys.

https://github.com/mividtim/pagination-test

Repro steps:

  • Clone the repo.
  • Run yarn or npm install.
  • Create a local DB and a PG user/password for the sample to own.
  • Copy sample.env to .env.
  • Edit the values in .env to point to your local DB
  • Run yarn start:dev
  • Hit http://localhost:3000 to generate the DB table. The output should be an object with an empty construed array.
  • Run yarn migration:up to populate the construed table with some test data.
  • Refresh http://localhost:3000 to see the new data (7 values in the construed property)
  • Hit http://localhost:3000?limit=3 to run into the error.

@benjamin658 benjamin658 added the bug Something isn't working label Nov 26, 2020
@benjamin658
Copy link
Owner

@mividtim thanks for the steps to reproduce an error.

I will try to fix it in my free time, if you already know how to fix it, please give me some hints or feel free to send a PR.

@mabilbao
Copy link

Hey guys! it's happening exactly the same. Any updates about it?

Thank you! 😄

@mividtim
Copy link
Author

mividtim commented Mar 4, 2021

Hey there! No update. I basically skipped cursor pagination and am using page lengths and numbers, which is inferior, but workable if your data doesn't move fast. I wish this worked, but I didn't invest the time to make it work.

@judehunter
Copy link

it's a shame this is still an issue...
unfortunately this makes this library unusable to me

@benjamin658
Copy link
Owner

benjamin658 commented Apr 14, 2021

Hey guys, sorry for the late reply.

@mividtim @judehunter actually I have already fixed the naming strategy issue a few months ago,
if you don't mind giving it a try, please upgrade to v0.6.0.

cc22879
c2dcc64

#19

@judehunter
Copy link

judehunter commented Apr 14, 2021

@benjamin658 at the time of my comment (9 days ago), I was using the latest version (which I believe was 0.6.0)
It was still an issue for me.

I might be able to provide some more detail later, but I have since switched to limit/offset pagination instead.

@benjamin658
Copy link
Owner

Hi @judehunter, thanks for your help.

Since I still cannot reproduce the issue, it would be great if you can provide some more details.

By the way, PR for fixing this issue is welcome.

@benjamin658
Copy link
Owner

Hi @mabilbao @mividtim @judehunter @miohtama ,

I think I finally found the root cause, it's ts-node issue: TypeStrong/ts-node#511

For some reason ts-node Reflect.getMetadata of Date reflection is broken, it returns Object instead of Date.
Currently you can use the --type-check flag to workaround if you are using ts-node to run you app.

I will try to fix the function

export function encodeByType(type: string, value: any): string | null {
to handle unexpected reflection issue.

@mividtim
Copy link
Author

We use tsc to compile, and run with node itself, and I was seeing object from the reflect call...

@benjamin658 benjamin658 linked a pull request Apr 15, 2021 that will close this issue
@benjamin658
Copy link
Owner

Sorry to keep you waiting so long.

I just fixed the issue, please upgrade to v0.6.1.

@Snowdrop-ctrl
Copy link

Snowdrop-ctrl commented Sep 27, 2021

Error: unknown type in cursor: [boolean]true
at Object.encodeByType (/home/nitesh/WebstormProjects/tw-backend/node_modules/typeorm-cursor-pagination/lib/utils.js:36:11)
at /home/nitesh/WebstormProjects/tw-backend/dist/utils/paginator/pagination.js:113:35
at Array.map ()
at Paginator.encode (/home/nitesh/WebstormProjects/tw-backend/dist/utils/paginator/pagination.js:111:14)
at Paginator.paginate (/home/nitesh/WebstormProjects/tw-backend/dist/utils/paginator/pagination.js:45:41)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async PostsService.getAllPosts (/home/nitesh/WebstormProjects/tw-backend/dist/services/posts.service.js:159:56)
at async PostsController.all (/home/nitesh/WebstormProjects/tw-backend/dist/controllers/posts.controller.js:14:42)

any solution for this boolean

@byteHulk
Copy link

it's a shame this is still an issue...
version 0.10.0

@mividtim
Copy link
Author

@byteHulk This might get you a bit further down the road... https://github.com/mividtim/typeorm-cursor-pagination

@mividtim
Copy link
Author

Hmm, maybe not. I don't see my commit that fixed the issue. :-( I long lost that computer.

@kprice-ae
Copy link

Having the same issue mentioned in #16 (comment)

does not work with booleans or enum types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants