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

Print warning when schema type is a POJO with non-zero number of properties #8107

Closed
hardcodet opened this issue Aug 28, 2019 · 3 comments
Closed
Labels
developer-experience This issue improves error messages, debugging, or reporting

Comments

@hardcodet
Copy link

hardcodet commented Aug 28, 2019

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I have the following schema:

export const AuthorizationSchema = new mongoose.Schema({
    foo: {type: String, required: true },

    nested: {
        type: {
            xxx: { type: String, required: true },
            yyy: { type: String, required: true }
        },
        required: false
    },
    createdBy: {type: String, required: true }
});

If I save() an object I retrieved before, only the top-level properties are getting saved, not the nested one:

item.foo = "hello"; //will be saved
item.nested.xxx = "world" //won't be saved

Edit: This seems to be related to the type section I introduced to mark the nested type as optional. If I declare it like this, saving it works, but of course, the item is now required.

    nested: {
            xxx: { type: String, required: true },
            yyy: { type: String, required: true }
    },

Also, declaring the nested type as a schema of its own seems to work, so that's a fix that works:

nested: {
    type: NestedSchema,
    required: false
},

My understanding is that nested items in arrays are not automatically updated, but this isn't one.
A workaround would be to use markModified with the path, but I hope that's just a dirty hack ;)
I saw this reported in an earlier bug (see last comment on #6152) that was marked as resolved, but if that was fixed, it has resurfaced.

Mongoose version: 5.6.10 (I think), then upgraded to 5.6.11

@vkarpov15
Copy link
Collaborator

Your schema is incorrect. When you do nested: { type: { xxx: String } }, Mongoose considers that equivalent to nested: { type: {} } and makes nested a mixed path. You should do nested: { type: NestedSchema } like you suggested.

We should at least print a warning when this happens, it's a common mistake.

@vkarpov15 vkarpov15 changed the title Single nested properties not updated on save Print warning when schema type is a POJO with non-zero number of properties Sep 4, 2019
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Sep 4, 2019
@vkarpov15 vkarpov15 added the developer-experience This issue improves error messages, debugging, or reporting label Sep 4, 2019
@hardcodet
Copy link
Author

Thanks for the context, Valeri!

(I'll leave the ticket open since you labeled it - as far as I'm concerned, I'm all good :)

@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.8.0 Oct 14, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.8.0, 5.x Unprioritized Nov 9, 2019
@vkarpov15
Copy link
Collaborator

We don't need this anymore, because in Mongoose 6 nested: { type: { xxx: String } } is equivalent to nested: { type: new Schema({ xxx: String }) }. So nested will be a subdocument.

@vkarpov15 vkarpov15 removed this from the 6.x Unprioritized milestone Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

No branches or pull requests

2 participants