为什么没有将关系添加到模型中的 loopback.io 对象中

Why are relations not being added to my loopback.io objects in the models?

本文关键字:loopback io 对象 模型 关系 添加 为什么      更新时间:2023-09-26

我对Loopback和NodeJS很陌生,所以请告诉我是否有"节点方式"来做我缺少的事情。我决定编写一个基本的应用程序来尝试了解更多信息。

我有两个模型,"用户信息"和"服装文章"。我创建了一个从UserInformation到ClothingArticle的"hasMany"关系。

作为基本测试,我想向UserInformation添加一个远程方法,以获取有关ClothingArticles的建议。但是,我似乎无法访问与服装文章相关的任何内容。我将代码添加到公共/模型/用户信息.js文件中,以尝试检索有关关系的信息,但不确定这是否是正确的放置位置。

我的代码在下面,你能帮忙吗?

通用/型号/用户信息.js:

module.exports = function(UserInformation) {
    get_methods = function(obj) {
        var result = [];
        for(var id in obj) {
            try {
                if(typeof(obj[id]) == "function") {
                    result.push(id + " (function): "); //+ obj[id].toString());
                }
                else
                    result.push(id + ": "); // + obj[id].toString());
            }
            catch (err) {
                result.push(id + ": inaccessible");
            }
        }
        return result;
    }
    // This doesn't anything about my new relations?
    console.log(get_methods(UserInformation.prototype));
    UserInformation.recommendations = function(source, callback) {
        var response = "I don't have any recommendations.";
        var test_function = UserInformation.findById(3, function(err, instances) {
            if(err) return console.log("Errors: " + err);
            console.log("Instances: " + String(instances));
            // Nothing here either about the relations.
            console.log(get_methods(UserInformation));
            console.log(UserInformation.app);
            /*
            instances.clothingArticles.create({
                id:92,
                colors:['red','blue']
            });
    */
            console.log("Created a new clothing article.");
        });
        console.log (response);
        callback(null, response);
    }
    UserInformation.remoteMethod(
        'recommendations',
        {
            accepts: [
                {arg: 'source', type: 'string'} // Used to mark the source (closet, generic, etc)
            ],
            http: {path: '/recommendations', verb: 'get'},
            returns: {arg: 'recommendations', type: 'string'}
        }
    );
};

common/models/user-information.json:

{
  "name": "UserInformation",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "properties": {
    "birthday": {
      "type": "date"
    },
    "id": {
      "type": "number",
      "id": true,
      "required": true
    },
    "eye_color": {
      "type": "string"
    },
    "hair_color": {
      "type": "string"
    },
    "weight": {
      "type": "string",
      "comments": "pounds"
    },
    "height": {
      "type": "number",
      "comments": "inches"
    }
  },
  "validations": [],
  "relations": {
    "clothingArticles": {
      "type": "hasMany",
      "model": "ClothingArticle",
      "foreignKey": "owner_id"
    }
  },
  "acls": [],
  "methods": []
}

common/models/clothing-article.json:

{
  "name": "ClothingArticle",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "properties": {
    "id": {
      "type": "number",
      "id": true,
      "required": true
    },
    "colors": {
      "type": [
        "Color"
      ],
      "required": true
    },
    "owner_id": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

我建议从我们的入门示例开始,然后通过此处的教程系列逐步完成:https://github.com/strongloop/loopback-example

您提出的问题在整个示例中都有答案(即模型关系)。要回答您的问题,如果您正确定义了关系,您应该能够通过点访问该关系。

...
UserInformation.ClothingArticle...
...

有关详细信息,请参阅 http://docs.strongloop.com/display/LB/HasMany+relations。