为什么没有将关系添加到模型中的 loopback.io 对象中
Why are relations not being added to my loopback.io objects in the models?
我对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。
相关文章:
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- 如何使用Socket.io将命令从客户端发送到服务器
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- Socket.IO未定义变量
- Socket.io on Mozilla Rhino
- 如何在Ionic2测试版中包含Ionic.io服务
- socket.io发射三次
- AngularJS$q承诺使用socket.io
- Atom.io中AngularJS的方法完成
- 如何知道使用socket.io订阅/取消订阅频道
- 可以't连接客户端和服务器import.io
- 通过socket.io发射给特定的人
- Loopback AngularJS JavaScript SDK - Gruntfile plugin
- 如何在使用loopback REST API时更改gulp-angular项目的端口号
- 通过socket.io从浏览器流式传输视频
- 通过node.js和socket.io向特定用户发送数据
- 为什么没有将关系添加到模型中的 loopback.io 对象中
- 使用loopback.io创建一个新项目