Correct way to use pre('remove')?

17 views
Skip to first unread message

Vinaya Sathyanarayana

unread,
Jun 10, 2017, 11:48:20 PM6/10/17
to Keystone JS
Hello,

I have a need to prevent deletion if a record is locked. Sample code is given below.
I need to 
a. flash a message that Record is Locked and cannot be deleted --> Not working : says req object is undefined
b. prevent deletion --> (i) The record is not getting deleted (good). (ii) Error message is not getting displayed (iii) mongoose error stack is thrown : how to catch and handle the error

Order.schema.pre('remove', function(next) {
    if (this.orderstate == CONST_LOCKED) {
        // record already locked - do not save - provide a message
        debug ('Already Locked');
        //req.flash('error', 'Already Locked Order!');
        var delerr = new Error('Cannot Delete - Locked Order');
        //return (delerr); // Do not delete
        next (delerr);
    }
    else {
        next ();
    }
});


Reply all
Reply to author
Forward
0 new messages