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

Unexpected validation on save #110

Closed
lzl124631x opened this issue Oct 31, 2019 · 4 comments
Closed

Unexpected validation on save #110

lzl124631x opened this issue Oct 31, 2019 · 4 comments

Comments

@lzl124631x
Copy link

// mongo 3 does't support validation when update. So instead of findOneAndUpdate, use findOne + save.
UserInfo.findOne({ userId: userInfo.userId })
    .then((ui) => {
        ui = Object.assign(ui, userInfo);
        return ui.save();
    })

I'm using the above code to do update with validation. Without this plugin it works fine. But if I add this plugin, it failed with errors about all the unque fields, including _id.

Is this expected? Any workaround? Thanks

@m-weeks
Copy link
Contributor

m-weeks commented Oct 31, 2019

If you trying doing ui.set(userInfo) instead of Object.assign does it still fail?

@lzl124631x
Copy link
Author

UserInfo.findOne({ userId: userInfo.userId })
    .then((ui) => {
        ui.set(userInfo);
        return ui.save();
    })

It still fail with ui.set(userInfo)... Same error. Even the _id of the userInfo doesn't change. I don't this is expected?

@lzl124631x
Copy link
Author

I ended up with using findOneAndUpdate + { runValidators: true, context: 'query' } which is mentioned in the README. Thanks for the help!

manglynho added a commit to manglynho/blogs-list that referenced this issue Feb 12, 2022
Sorry i have to downgrade the  mongoose unique validator package due to this issue ...
mongoose-unique-validator/mongoose-unique-validator#110
@eskiimo
Copy link

eskiimo commented Feb 19, 2022

mongoose-unique-validator has a bug in it so that when you have multiple users it detects the reference to _id in another schema. Then it decides that _id is no longer unique and will not let any user post a new Document.

The fix at the moment is to re-roll mongoose and mongoose-unique-validator to v^5.11.13 and v^2.0.3 respectively.

There is a current GitHub issue posted here.

#131

Answered By – James Beswick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants