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

Can't use mongoose plugin based on pre with patch or put #178

Closed
dortamiguel opened this issue Apr 7, 2017 · 6 comments
Closed

Can't use mongoose plugin based on pre with patch or put #178

dortamiguel opened this issue Apr 7, 2017 · 6 comments

Comments

@dortamiguel
Copy link

I'm using this plugin for generating an slug https://github.com/mindblaze/mongoose-url-slugs
(I also tried 10 different plugins with the same result)

I have this when I do a "get" to http://localhost:3030/posts/58e77fb2bdce58efa796f183

{
	"_id": "58e77fb2bdce58efa796f183",
	"description": "A description for the First Post",
	"title": "First Post",
	"createdAt": "2017-04-07T12:01:54.976Z",
	"slug": "first-post"
}

If I do a "put" (update in hooks) to the same url with this fields:

{
	"title": "new title",
	"description" : "new description"
}

I get this response:

{
	"_id": "58e77fb2bdce58efa796f183",
	"title": "new title",
	"description": "new description"
}

"createdAt" and "slug" disappeared, of course with "patch" this doesn't happens but then I can't generate my slug. 😕

How I can solve this? Should be good to force an update to patch? This can be possible?

I think the problem is that path is not calling the mongoose event 'validate'

@jamesjnadeau
Copy link
Contributor

When you do your PUT/update request, you need to upload the entire record you'd like to save. When you PUT just the title and description field, the whole "row" is being updated, not merging in new results.

After your PUT request, the record will only contain the "title" and "description" fields. You've essentially deleted them as far as the servers concerned because you didn't send them over again. Maybe a PATCH operation is what you were looking for?

Try making the slug and createdAt fields required and I think it will be more obvious what is happening.

@dortamiguel
Copy link
Author

Yes, now I'm only using patch.

The thing is that only on post the slug is being created, patch ignores the pre.('validate', function) of the plugin.

@dortamiguel
Copy link
Author

I created a plugin that works with mongoose and feathers that solves this issue

https://github.com/ellipticaldoor/slugify-mongoose

@ekryski
Copy link
Member

ekryski commented Apr 11, 2017

@ellipticaldoor this is why we usually do validation in hooks instead of relying on mongoose. The pre validators are clunky with mongoose and using hooks keeps you relatively DB agnostic.

@dortamiguel
Copy link
Author

@ekryski yes! I see it more clearly now. The problem for me in this case was that I had to share the schema with other app.

@AdamGerthel
Copy link

I have the exact same problem, see #395

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

4 participants