Closed ApayRus closed 6 years ago
I would say it's better to use the normal callback functionality of update
in this case.
Articles.update(...., (error, result) => {
if (error) {
// handle error
return;
}
FlowRouter.go('article', { id: result.id });
});
Then I should repeat this redirection in 10 times. Is better to do this once in "after update" hook.
I'm just thinking out loud here, but I do believe the after hook don't run whenever an insert fails. i.e. after hooks should only run on successful inserts/updates/upserts/finds etc.
after insert i have other hooks. I need only after update. And it runs after errors too, and redirect me from the form... (I'm using SimpleSchema and AutoForm )
The only thing I can think of that might help you is to use this.previous
to compare the updated doc doc
with this.previous
and if they are the same then something went wrong and you shouldn't redirect.
https://github.com/matb33/meteor-collection-hooks#afterupdateuserid-doc-fieldnames-modifier-options
Oh yeah, you also have this.affected
which is the return value of the original update
call. So that variable should tell you if any documents where updated.
If you call the update method and provide a callback function this.err
should also be available in the after.update
hook which should contain the error if there were any. If you do not provide a callback function when you do the update call then an error should be thrown if your on the server making sure no after update hooks are called.
I might be able to change the logic a bit to make sure there's always a this.err
if you call update in client code without providing a callback function.
@zimme Thanks for answers! I tried to make what you said:
Articles.update({_id: doc._id},
doc.modifier,
{upsert: true},
function(error, result){
console.log('my error', error);
console.log('my result', result);
},
);
But there is no errors )
my error undefined
my result 1
I grasped the idea with your help. I will find the way to fix it in other ways. thanks a lot.
I want to redirect from edit form to article only if it is successful. But now it's a big broblem for me. I did big work on editing article, but it was wasted and I'm now at old version... Here is the hook: