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

UPDATE doesn't work in mongoose #2221

Closed
thomsa opened this issue Sep 11, 2016 · 2 comments
Closed

UPDATE doesn't work in mongoose #2221

thomsa opened this issue Sep 11, 2016 · 2 comments
Labels
Milestone

Comments

@thomsa
Copy link

thomsa commented Sep 11, 2016

Hi!

I've encountered an error while trying to update entities.

This is the generated code:
findOneAndUpdate(req.params.id, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
It was always updating the first element in the collection, because it's not specified what field it should compare.
As in the mongoose documentation:
query.findOneAndUpdate(conditions, update, options)
The condition is not really a condition in the generated code.

I had to change it to the code below and it started to work as expected
entity.findOneAndUpdate({"_id" : req.params.id }, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()

Has anybody encountered this problem, or is it written in some documentation that this is what we have to do if we want it to work? I haven't found it in any docs though.

@dnewman-polar3d
Copy link

I too have encountered this in the past. However, I do not recall if it was in auto-generated code or simply my own mistake. That said, the call

findOneAndUpdate(req.params.id, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()

is certainly incorrect; the first call argument must be a condition and in this case should be as you show in the second statement. It is annoying that mongoose doesn't detect this and treat it as an error.

@thomsa
Copy link
Author

thomsa commented Sep 11, 2016

If someone will verify that this is a bug, I can create a pull request later on.

@Awk34 Awk34 added the bug label Sep 13, 2016
@Awk34 Awk34 added this to the Soon milestone Sep 13, 2016
@Awk34 Awk34 closed this as completed in 1c2a219 Sep 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants