Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不将关系添加到模型中的loopback.io对象中?_Javascript_Node.js_Loopbackjs - Fatal编程技术网

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

Javascript 为什么不将关系添加到模型中的loopback.io对象中?,javascript,node.js,loopbackjs,Javascript,Node.js,Loopbackjs,我对Loopback和NodeJS非常陌生,所以请告诉我是否有一种节点方式可以做我所缺少的事情。我决定写一个基本的应用程序来尝试了解更多 我有两个模型,“用户信息”和“ClothingArticle”。我已经创建了一个从UserInformation到ClothingArticle的“hasMany”关系 作为一个基本测试,我想向UserInformation添加一个远程方法,以获得关于衣服的建议。然而,我似乎无法接触到任何与衣物相关的物品。我在common/models/user-inform

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

我有两个模型,“用户信息”和“ClothingArticle”。我已经创建了一个从UserInformation到ClothingArticle的“hasMany”关系

作为一个基本测试,我想向UserInformation添加一个远程方法,以获得关于衣服的建议。然而,我似乎无法接触到任何与衣物相关的物品。我在common/models/user-information.js文件中添加了代码,以尝试检索有关关系的信息,但不确定这是否是放置关系的正确位置

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

common/models/user-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'}
        }
    );
};
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": []
}

我建议您从我们的教程开始,在这里学习本系列教程:

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

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

有关更多信息,请参阅。

感谢您的回复。我通读了文档和示例,但我发现没有一个示例通过关系实际使用数据。他们在JSON模型中声明了它,但从未实际访问过样本中的任何地方。我还对UserInformation运行了get_方法,没有看到对ClothingArticle的任何引用。关于如何对其进行更多调试,有什么想法吗?我同意,我们需要一个示例回购协议来专门演示模型关系,而不需要休息。现在,你可以在这个回购协议中看到一个例子,你可以看到我们使用用户与被访问项目的关系。另外,请查看“common/models/*.json”并查看定义的关系。这是我想要的更多内容,谢谢!代码基于server/boot,但是您可以直接使用commond/model/.js文件中的模型关系吗?或者该文件只是用于描述全局模型,而不是使用模型的实例?>可以直接在commond/model/.js文件中使用这样的模型关系吗?是的,就是这样。在任何可以访问app.models.ModelName的地方,都可以访问其关系;如果有错误的话,我认为是错误。我最终放弃了使用Loopback.io,改为使用Mean.js。文档和示例更清晰,至少对我来说是这样。