Using operation hook "before delete"

229 views
Skip to first unread message

Partap Davis

unread,
Jul 30, 2015, 8:36:07 PM7/30/15
to LoopbackJS
Is there any way to reject the deletion of a model by setting or returning a value in the "before delete" operation hook?

Partap Davis

unread,
Jul 30, 2015, 9:30:40 PM7/30/15
to LoopbackJS, chee...@gmail.com
Aha...it seems that throwing an error in the hook does the trick.

Partap Davis

unread,
Aug 1, 2015, 9:44:04 PM8/1/15
to LoopbackJS, chee...@gmail.com, Raymond Feng, ir...@strongloop.com
I should make a note that this is not quite the right thing to do.  

Looks like you need to call done() with an error to abort the delete, or whatever op...(Document me!) 

...For some reason, when I saw "done" I was thinking of it being a different sort of callback than "cb" with implicit error for first argument... no idea why, but maybe it should be explicitly mentioned that passing an error to "done" will abort... 

Operation hooks also support promises now...(Document me!)

Without being in a promise block, throwing an error like I said will crash the server.  

If a promise is returned from the hook function, it will automatically call next() or next(err) if resolved or rejected.  

If you do this in a then statement in the hook, then next() will be called twice. 
Which could be bad.

Nathan Stowell

unread,
Aug 3, 2015, 12:20:18 AM8/3/15
to LoopbackJS, chee...@gmail.com, ray...@strongloop.com, ir...@strongloop.com
would you be so kind as to post an actual example of this?  I am trying to do the same thing but can't quite figure out how to stop the deletion of a model if a certain condition exists.  for instance here is my "if" statement inside of a simple query which itself is inside a "before delete" hook

if (subscriptions.length > 0) {
    console.log("Client has an active subscription, cannot delete");
    //How do I stop the deletion of this Client?
}else{
    next();
}

Thank you in advance for any help you can offer!

Partap Davis

unread,
Aug 3, 2015, 12:51:15 PM8/3/15
to LoopbackJS, chee...@gmail.com, ray...@strongloop.com, ir...@strongloop.com, n8st...@gmail.com
I've been doing something like this...
Set the status code to something appropriate, otherwise it comes back as a 500 error (if it was a remote call)

if (subscriptions.length > 0) {
  //Stop the deletion of this Client 
  var err = new Error("Client has an active subscription, cannot delete");
  err.statusCode = 400;
  console.log(err.toString());
  next(err);
}else{
  next();
}


Rand McKinney

unread,
Aug 4, 2015, 8:06:39 PM8/4/15
to LoopbackJS, chee...@gmail.com, ray...@strongloop.com, ir...@strongloop.com, n8st...@gmail.com
Looks like you need to call done() with an error to abort the delete, or whatever op...(Document me!) 
...For some reason, when I saw "done" I was thinking of it being a different sort of callback than "cb" with implicit error for first argument... no idea why, but maybe it should be explicitly mentioned that passing an error to "done" will abort... 

I'll add this to the docs; thanks for brining it to my attention.


Operation hooks also support promises now...(Document me!)

There's an issue to document Promise support https://github.com/strongloop/loopback/issues/1409
The engineering work has been in progress for a long time, and I've been waiting until it's complete to start documentation.  I don't believe it is 100% done yet, but I'll find out if it's far enough along to start docs work.

Thanks,
Rand

Andy Kellerstrass

unread,
May 2, 2016, 7:01:18 PM5/2/16
to LoopbackJS
Is it possible to stop the deletion in the "before delete" operation hook without passing an error? I have looked at the datasource connector source code and there does not appear to be a way currently. 

I may be approaching my problem the wrong way. I want to "soft delete" the data for some models, and I figured the best way to do that without hacking a bunch of routes would be in the "before delete" hook. I want to stop the "hard delete" in most cases and instead set an "isDeleted" bool on the object. I also need to be able to fully delete the row for unit tests and such, so I added a flag to the delete options to bypass the soft delete.

For my use case, as far as the user is concerned I want them to think the delete went perfectly, so returning an error from the destroy call even if it has the statusId set to 200 is a little counterintuitive.

Is there another way to go about this, or would the community be open to a small change in the datasource juggler to allow for non-error delete overrides?

Thanks,

Andy

Rastio Hodul

unread,
Sep 15, 2016, 6:52:10 PM9/15/16
to LoopbackJS
What about async nature of .find()??

Would this code be proper?
Person.observe('before delete', function (ctx, next) {
    Person.find({where: context.where}, function (err, people) {
        if (err) next(err);

        for (var i = 0; i < people; i++) {
            // stop the deletion of built in users
            if (people.isBuiltIn) {
                var stopError = new Error('Built in user cannot be deleted!');
                stopError.statusCode = 400;
                console.log(stopError.toString());
                next(stopError);
            }
        }
        next();
    });
});
Reply all
Reply to author
Forward
0 new messages