【问题标题】:Removing base model relations in Loopback 3在 Loopback 3 中删除基本模型关系
【发布时间】:2019-09-24 14:05:34
【问题描述】:

我在尝试删除关系时遇到了一点麻烦 来自使用另一个自定义模型作为其基础的模型。假设以下 设置

// Item Model, item.json
...
"relations": {
    "parent": {"type": "belongsTo", "model": "Holder"}
}
...
// Special Item Model, item.json
...
"base": "Item",
"excludeBaseProperties": ["parentId"],
...
"relations": {}
...

当我查看 Special Item 模型时,我发现 parentId 仍然存在并且 它甚至出现在关系中。

在环回 3 中从子模型中删除父模型关系的正确方法是什么?

【问题讨论】:

    标签: loopbackjs


    【解决方案1】:

    我觉得现在发布这个问题有点傻,因为我在发布它一段时间后想出了一种解决方法。

    我在这里发布我的解决方案,如果有人可以查看它,将不胜感激:

    1. 创建一个名为clear-relations.js的模块
    2. 在其中使用以下代码 -
    'use strict';
    
    module.exports = function(Model) {
        // Get the attribute names of the inherited relations
        // and then explicitly delete them
    
        Object.keys(Model.settings.relations).forEach(relation => {
            delete Model.settings.relations[relation];
        });
    }
    
    1. 如果您的模型还没有 .js 文件,请创建一个,然后使用以下代码
    const ClearRelations = require("path/to/clear-relations.js");
    
    module.exports = function(MyModel) {
        ClearRelations(MyModel);
    }
    

    一旦我弄清楚了,似乎很容易。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2018-04-15
      • 1970-01-01
      相关资源
      最近更新 更多