-
Notifications
You must be signed in to change notification settings - Fork 127
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
Model.pre('save', fn) is not working as documented #485
Comments
This works fine for me
|
Ok, so the problem must be with tastypie-rethink.. I suppose it's not handling errors properly. |
It depends what hooks. Validate is called twice for a save (once before, On Sun, Apr 24, 2016, 13:09 Paolo Furini [email protected] wrote:
|
While Anyway, it looks like
and ended up in an infinite loop.. I also tried querying using |
The right way to enforce uniqueness is to set the field as a primary key. That's the normal and safe way to do it. If you don't want to set it as a primary key, you need to maintain a distributed lock, but that's kind of pain to do properly. What you wrote seems reasonable to me if you want a weak uniqueness, but I'm not sure where your inifnite loop come from, I would need to see more code for that. |
And yes the pre hook save is called once before saving a document. |
I fixed some code in tastypie-rethink, but the problem with the |
You can use the |
Unfortunately the Can I ask why a pre validate method should be called after save? It drives me crazy.. :-/ |
The hook is triggered twice because thinky validates the input before saving it, and validate the output before returning it. There is a debate on whether thinky should validate when retrieving a document (I somehow can't find it right now, but there is). The solution is probably to make it an option but it's not available at the moment. |
Hi, sorry to bother again, but with this code:
if I return an error (using If I change the code above using the promise pattern
then both the The only change in my code is the above, and the behaviour is consistent. There's definitely something wrong here, let me know if you're able to reproduce the issue. P. |
Can you provide a script the trigger your issue? I have a hard time understanding what the issue is with your snippet |
Sure, see https://ide.c9.io/nexbit/thinky-issue485 To avoid flooding the console with connection errors, run the rethinkdb instance I pre-installed in the container (go to the terminal tab on the bottom, and run the usual Paul |
I can't access your snippet, can you make it a gist? |
Here it is: https://gist.github.com/nexbit/a805d1bf2ae7e3cdd5c87907f19641f2 BTW, the workspace on cloud9 is public, you only need to create a free account. If you don't know c9, it's wonderful to quickly test and share complete working projects, or demonstrate issues in a full dev env (e.g. in that workspace there's a working instance of rethinkdb, and a full node/npm project, so you can test the code as if it was on your machine). P. |
Thanks for the snippet. |
I have something like:
This is what official docs suggest, that is calling next with an error instance, and that should invalidate the save. But the error get uncaught:
and the request never returns to the client.
The behaviour is different with pre('validate'), in that case calling next(new Error()) causes a 500 error to be returned to the client (I'd prefer a 400, but that's another issue).
Another issue is that every
pre
hook seems to be called multiple times (it happens with both 'validate' and 'save' hooks). This is not only inefficient, but dangerous when you try to perform consistency checks that depend on other tables.How one is supposed to validate before saving and returning a 400 on error? And hopefully avoid calling hooks multiple times?
Thx
The text was updated successfully, but these errors were encountered: