Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
658 stars 90 forks source link

How to handle errors after.update? #228

Closed ApayRus closed 6 years ago

ApayRus commented 7 years ago

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:

Articles.after.update(function(userId, doc, fieldNames, modifier, options){
            //if only update was successful???
            FlowRouter.go('article',{ id: doc._id});
});
zimme commented 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 });
});
ApayRus commented 6 years ago

Then I should repeat this redirection in 10 times. Is better to do this once in "after update" hook.

zimme commented 6 years ago

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.

ApayRus commented 6 years ago

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 )

zimme commented 6 years ago

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

zimme commented 6 years ago

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.

zimme commented 6 years ago

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.

ApayRus commented 6 years ago

@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.