Javascript strong父级中关系的循环属性
我不知道我是瞎了还是怎么了,但我怎么能做到以下几点: 我有一个Javascript strong父级中关系的循环属性,javascript,node.js,loopbackjs,strongloop,Javascript,Node.js,Loopbackjs,Strongloop,我不知道我是瞎了还是怎么了,但我怎么能做到以下几点: 我有一个User模型,它与UserData模型有一个hasOne关系。我只希望UserData的一个属性直接出现在User的结果中 User中的关系如下所示: "relations": { "userData": { "type": "hasOne", "model": "UserData" } } 以及用户中的默认范围: "scope": { "include": "userData" } { "name
User
模型,它与UserData
模型有一个hasOne
关系。我只希望UserData
的一个属性直接出现在User
的结果中
User
中的关系如下所示:
"relations": {
"userData": {
"type": "hasOne",
"model": "UserData"
}
}
以及用户
中的默认范围:
"scope": {
"include": "userData"
}
{
"name": "ChiliUser",
"base": "ChiliUserData",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"table": "person"
}
},
"properties": {
"id": {
"type": "number"
},
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"vorname": {
"type": "string"
},
"name": {
"type": "string"
},
"spitzname": {
"type": "string"
},
"strasse": {
"type": "string"
},
"plz": {
"type": "number"
},
"ort": {
"type": "string"
},
"geolat": {
"type": "string"
},
"geolng": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
因此,一个用户的结果是:
[
{
"id": 5,
"email": "example@example.com",
"name": "Example",
"userData": {
"id": 5,
"birthdate": "1971-09-06T00:00:00.000Z"
}
}
]
但我想要的是:
[
{
"id": 5,
"email": "example@example.com",
"name": "Example",
"birthdate": "1971-09-06T00:00:00.000Z"
}
]
我怎样才能做到这一点
编辑:
两个模型定义:
用户
:
"scope": {
"include": "userData"
}
{
"name": "ChiliUser",
"base": "ChiliUserData",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"table": "person"
}
},
"properties": {
"id": {
"type": "number"
},
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"vorname": {
"type": "string"
},
"name": {
"type": "string"
},
"spitzname": {
"type": "string"
},
"strasse": {
"type": "string"
},
"plz": {
"type": "number"
},
"ort": {
"type": "string"
},
"geolat": {
"type": "string"
},
"geolng": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
用户数据
:
{
"name": "ChiliUserData",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"table": "person_data"
}
},
"properties": {
"id": {
"type": "number"
},
"person_id": {
"type": "number"
},
"birthdate": {
"type": "date"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
您是否使用内置的用户
模型?如果是,您可以使用base:“User”
使用UserData
模型简单地扩展它,基础架构将如您所愿:
{
"name": "UserData",
"base": "User",
"properties": {}
...
}
但是这种设置意味着您将在代码中使用UserData
而不是User
。由于内置的用户
模型会造成阻碍,因此我建议使用另一个词,如个人
或客户
,在这种情况下,它看起来像:
{
"name": "Person",
"base": "User",
"properties": {
...extra user data properties only...
}
...
}
这样,只有Person
将有所有额外的“用户数据”字段,但也将包括user
内部node\u模块/loopback/common/models/user.json中定义的所有字段/方法。如果您使用的是MySQL,Person
表将由User
和Person
中的字段组合而成,谢谢您的回答!不,我不使用内置用户模型。为了更好的可读性,我在问题中把它重命名了。在我的应用程序中,模型是ChiliUser
和ChiliUserData
。有没有其他方法可以做到这一点?因为在本例中,ChiliUser
模型是主要模型,我需要这样的模型。我认为通过使用“base:“ChiliUserData”
定义ChiliUser
模型,您可以获得所需的效果。我在上面的问题中添加了模型定义。当我现在为ChiliUser
执行默认的GET
方法时,StrongLoop会给我这个错误:ER\u BAD\u FIELD\u错误:“FIELD list”中的未知列“person\u id”
。除了添加“base”:“ChiliUserData”
,我还需要做一些额外的事情吗?您曾经创建过一个人物模型吗?该错误通常意味着loopback希望看到数据库中的person_id列,但尚未创建该列。它可能是一个您可以删除的工件,因为除非您显式地创建了与Person模型的关系,否则它没有意义。这两个模型都连接到MySQL数据库。ChiliUserData后面的表有三列:id、person\u id和生日。据我所知应该没问题。