父母关系中的强环性

我不知道我是否只是盲目或什么,但我怎么能做到以下几点:

我有一个与UserData模型hasOne关系的User模型。 我只想在User的结果中直接使用UserData一个属性。

User的关系如下所示:

 "relations": { "userData": { "type": "hasOne", "model": "UserData" } } 

User的默认范围:

 "scope": { "include": "userData" } 

所以一个User的结果是:

 [ { "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" } ] 

我怎样才能做到这一点?

编辑:

这两个模型定义:

ChiliUser

 { "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": [] } 

ChiliUserData

 { "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": [] } 

你在使用内置的User模式吗? 如果你是这样,你可以简单地使用你的UserData模型来扩展它,使用base: "User" ,而底层模式将是你想要的:

 { "name": "UserData", "base": "User", "properties": {} ... } 

但是这个设置意味着你会在你的代码中使用UserData不是User 。 由于内置的User模型妨碍了我的build议,所以我build议使用不同的单词,如PersonCustomer ,在这种情况下,它看起来像:

 { "name": "Person", "base": "User", "properties": { ...extra user data properties only... } ... } 

这样,只有Person将拥有所有额外的“用户数据”字段,但也会包含在node_modules/loopback/common/models/User.jsonUser定义的所有字段/方法。 如果您使用的是MySQL, Person表将使用UserPerson的字段的组合来创build。