question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incompatibility with Typescript 4.8.2 - Schemas that use query population (ObjectId and Ref) fail with serialization errors

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.4

Node.js version

14.17.6

MongoDB server version

4.2 but N/A

Description

When using a schema that uses query population and has a property defined with type set to ObjectId and a ref set. You get serialization errors when building. This seems to have started occurring recently, I tested with Typescript 4.8.2 but suspect its anything 4.8.x

src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serial ized because its property ‘[iterator]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serialized because its property ‘[species]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serialized because its property ‘[toStringTag]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serialized because its property ‘[iterator]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serialized because its property ‘[species]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ src/person/schemas/person.schema.ts:23:14 - error TS4118: The type of this node cannot be serialized because its property ‘[toStringTag]’ cannot be serialized. 23 export const PersonSchema = new Schema( ~~~~~~~~~~~~ Found 6 error(s).

Steps to Reproduce

Using typescript version 4.8.2 have the following schema and try build:

export const PersonSchema = new Schema(
    {
        information: InformationSchema
})

export const InformationSchema = new Schema({
  foo: { type: Schema.Types.ObjectId, ref: 'PersonModel' }
})

Expected Behavior

A successful build. Instead you get the errors mentioned above

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

1reaction
HiiiiDcommented, Sep 20, 2022

@piercy Can you try with this fix that I’ve made? https://github.com/HiiiiD/mongoose/tree/fix-path-types

0reactions
vkarpov15commented, Oct 2, 2022

I can confirm this is an issue, and the issue is fixed by #12352. This will be fixed when we release 6.7.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

field with Types.ObjectId has typescript error in Schema #12364
Prerequisites I have written a descriptive issue title Mongoose version 6.5.4 Node.js version 16.14.2 MongoDB version doesn't matter ...
Read more >
Mongoose TypeScript ObjectId casting - Type 'string' is not ...
When I try to cast string[] to Schema.Types.ObjectId[] array, the error goes away but this cannot be the solution, since it crashes at...
Read more >
How To Link Mongoose And Typescript for a Single Source of ...
1. Keeping a typescript interface and a mongoose schema in sync. The typical way to declare a user model looks something like this:...
Read more >
MongoDB | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
Populate with TypeScript - Mongoose
ObjectId, ref: 'Child' }, name: String })); interface Child { name: string; } const childSchema: Schema = new Schema({ name: String }); const...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found