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

Issue with createdAt immutability after updating to v6 #10807

Closed
vtrphan opened this issue Sep 29, 2021 · 10 comments
Closed

Issue with createdAt immutability after updating to v6 #10807

vtrphan opened this issue Sep 29, 2021 · 10 comments
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Milestone

Comments

@vtrphan
Copy link

vtrphan commented Sep 29, 2021

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I'm getting this error after updating to v6
TypeError: Invalid schema configuration: Trueis not a valid type at pathcreatedAt.immutable. See http://bit.ly/mongoose-schematypes for a list of valid schema types.
If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
I expect no error just like the case with v5
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 14
Mongoose: 6.0.8

Please help

@sean-daley
Copy link

Looks like the same (or similar) issue described here #10761 which seems to be fixed in mongoose 6.0.8.

@vtrphan
Copy link
Author

vtrphan commented Sep 29, 2021

so i've updated to 6.0.8 and the problem still persists

@sean-daley
Copy link

Actually, sorry. The person that submitted that defect was able to resolve the problem on their side so nothing was actually changed in mongoose to resolve it.

@vtrphan
Copy link
Author

vtrphan commented Sep 29, 2021

Please advise how i can solve this problem. Looks like that issue also has the "bug" tag, so i assume this is a bug on mongoose's side?

@IslandRhythms IslandRhythms added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 29, 2021
@IslandRhythms
Copy link
Collaborator

Modify the script below to demonstrate your issue

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({ name: String, age: Number}, {timestamps:true});

const Test = mongoose.model('Test', testSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();

    await Test.create({name: 'a', age: 1});
    await Test.create({name: 'b', age: 2});
    await Test.create({name: 'c', age: 3});
    console.log(await Test.find())
    
}

run();

@vtrphan
Copy link
Author

vtrphan commented Oct 1, 2021

i'm using discriminator with timestamps: true in nestjs (via nestjs/mongoose package), and got the following error
TypeError: Invalid schema configuration: Trueis not a valid type at pathcreatedAt.immutable. See http://bit.ly/mongoose-schematypes for a list of valid schema types.
Specify timestamps: true only at the base schema and not in the children schemas works, but when specifying timestamps: true also for the children schemas gave me that error.
Do i have to specify the option timestamps: true in every children schemas, or i only need to specify it once in the base schema and discrimination will take care of carrying the timestamps also to the children schemas ?

@vkarpov15
Copy link
Collaborator

@vtrphan you do need to specify timestamps: true to get timestamps in child schemas.

Without a code sample that demonstrates this issue, we're unlikely to be able to fix this issue.

@vtrphan
Copy link
Author

vtrphan commented Oct 4, 2021

@vkarpov15 i have tried to recreate the issue. It can be found here. Please open a new terminal and run with yarn start. Then comes the error TypeError: Invalid schema configuration: Trueis not a valid type at pathcreatedAt.immutable``
https://codesandbox.io/s/agitated-mountain-ibg1m?file=/src/core.module.ts

@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 Oct 4, 2021
@vkarpov15 vkarpov15 added this to the 6.0.13 milestone Oct 4, 2021
vkarpov15 added a commit that referenced this issue Nov 6, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 6, 2021
@vkarpov15
Copy link
Collaborator

I took a closer look and the issue is that you need to do @Prop({ type: Date }) instead of @Prop(Date). @Prop(Date) for some reason ends up registering a type property on Date equivalent to Date.type = Date;, which confuses Mongoose into thinking that createdAt is a nested property rather than a date.

We fixed this issue in b21a3d2, and will ship the fix in v6.0.13. In the meantime, just use @Prop({ type: Date })

@vtrphan
Copy link
Author

vtrphan commented Nov 7, 2021

@vkarpov15 thank you for the advice. Had it working now. Thanks for your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Projects
None yet
Development

No branches or pull requests

4 participants