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

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

Closed
2 tasks done
piercy opened this issue Sep 1, 2022 · 8 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@piercy
Copy link

piercy commented Sep 1, 2022

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

@daniandl
Copy link

daniandl commented Sep 2, 2022

Happening to me as well when my VS Code updated to 1.71

@dima-mamaev
Copy link

same thing

@thanglq-teraark
Copy link

Same issue

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Sep 14, 2022
@IslandRhythms
Copy link
Collaborator

import mongoose, { Schema, model, connect, PopulatedDoc, connection } from 'mongoose';

interface IGamer {
  gamerTag: string;
}

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  avatar?: PopulatedDoc<IGamer>;
}

const gamerSchema = new Schema<IGamer>({
  gamerTag: String
});

// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: { type: mongoose.Types.ObjectId, ref: 'Gamer' }
});

// 3. Create a Model.
const Gamer = model<IGamer>('Gamer', gamerSchema);
const User = model<IUser>('User', userSchema);

run().catch(err => console.log(err));

async function run() {
  // 4. Connect to MongoDB
  await connect('mongodb://localhost:27017/test');
  await connection.dropDatabase();

  const gamer = new Gamer({
    gamerTag: 'xXTestMasterXx'
  });

  await gamer.save();

  const user = new User({
    name: 'Bill',
    email: '[email protected]',
    avatar: gamer._id
  });
  await user.save();

  const test = await User.findOne().populate('avatar');

  console.log(user.email); // '[email protected]'
  console.log(test);
}

@piercy
Copy link
Author

piercy commented Sep 20, 2022

@IslandRhythms thanks for your response, you are correct in that your example seems to work.. potentially this is an issue with another library. The errors point at the schema, but this could be misleading.

I've created a reproduction repository, linked below. Instructions are on the read me, but essentially, npm install and npm run build should reproduce the issue.

I realize my reproduction code doesn't actually do anything. However, all I am interested in is a successful build.

https://github.com/piercy/mongoose-typescript-482-test

@HiiiiD
Copy link

HiiiiD commented Sep 20, 2022

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

@piercy
Copy link
Author

piercy commented Sep 20, 2022

@HiiiiD yes that does appear to solve the issue. Hopefully the PR for that gets accepted and then its resolved for a future version 👍

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Sep 30, 2022
@vkarpov15 vkarpov15 added this to the 6.6.4 milestone Sep 30, 2022
@vkarpov15
Copy link
Collaborator

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

@vkarpov15 vkarpov15 modified the milestones: 6.6.4, 6.7 Oct 2, 2022
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

7 participants