我有两个模特:
User.json
"relations": {
"items": {
"type": "hasMany",
"model": "Item",
"foreignKey": "userid"
},}Item.json
"relations": {
"user": {
"type": "belongsTo",
"model": "User",
"foreignKey": "userid"
},}回送生成此端点:
删除/User/{userid}/items/{itemid}
我想重写默认的删除方法,这样项目就不会被删除,只有属性active才会被设置为false。
我的问题是:如何覆盖此默认方法或防止删除记录?
我试过:
Item.once('attached',function(){
Item.destroyById = function(filter,auth, cb){
console.log('This is a overridden method')
}
})似乎没有被执行。
和:
Item.observe('before delete', function(ctx, next) {
var err = new Error("Not deleted");
next(err)
});它工作(项目没有删除),但错误抛出,我想避免。也许有一种方法可以将一些参数传递给next()方法?
发布于 2019-02-22 13:48:52
也许您可以为该操作实现连接器钩子,在那里可以动态地修改db查询。
var connector = Model.getDataSource().connector;
connector.observe('before execute', function(ctx, next) {
// preview your query and modify it as desired
console.log(ctx);
// so when you call next it will be executed in a shape you've created
next();
});这里有更多关于它的信息:https://loopback.io/doc/en/lb3/Connector-hooks.html
https://stackoverflow.com/questions/54828317
复制相似问题