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

时间:2015-01-20 01:22:05

标签: javascript node.js loopbackjs

我对Loopback和NodeJS很新,所以请告诉我是否有一个" Node方式"做我不想做的事。我决定写一个基本的应用程序来尝试学习更多。

我有两个型号,' UserInformation'和'服装文章'。我创造了一个'hasMany'从UserInformation到ClothingArticle的关系。

作为基本测试,我想为UserInformation添加一个远程方法,以获取关于ClothingArticles的推荐。但是,我似乎无法访问与ClothingArticles相关的任何内容。我在common / models / user-information.js文件中添加了代码来尝试检索有关关系的信息,但我不确定这是否是放置它的正确位置。

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

公共/模型/用户information.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'}
        }
    );
};

公共/模型/用户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": []
}

公共/模型/服装-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": []
}

1 个答案:

答案 0 :(得分:0)

我建议从我们的getting started example开始,并在此处完成教程系列:https://github.com/strongloop/loopback-example

你问的问题在整个例子中得到了回答(即模型关系)。要回答您的问题,如果您正确定义了关系,您应该能够通过点访问该关系。

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

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