-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
Happening to me as well when my VS Code updated to 1.71 |
same thing |
Same issue |
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);
} |
@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, 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 |
@piercy Can you try with this fix that I've made? |
@HiiiiD yes that does appear to solve the issue. Hopefully the PR for that gets accepted and then its resolved for a future version 👍 |
I can confirm this is an issue, and the issue is fixed by #12352. This will be fixed when we release 6.7.0. |
Prerequisites
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 aref
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.xSteps to Reproduce
Using typescript version 4.8.2 have the following schema and try build:
Expected Behavior
A successful build. Instead you get the errors mentioned above
The text was updated successfully, but these errors were encountered: