请求正文模式缺少属性/为什么我得到不完整的模式模型
有人可以帮助我了解Loopback4中的模型如何工作吗? 我定义了一个看起来像这样的模型:
@model()
export class ProductViewConfig extends BaseConfig {
@property({
type: 'string',
id: true,
generated: true,
})
_id?: string;
@property({
type: 'array',
itemType: 'object',
})
tiles: Array<TileOptions>;
constructor(data?: Partial<ProductViewConfig>) {
super(data);
}
}
export interface ProductViewConfigRelations {
// describe navigational properties here
}
export type ProductViewConfigWithRelations = ProductViewConfig & ProductViewConfigRelations;
它从看起来像这样延伸的baseconfig类:
@model({
settings: {
strict: true
}
})
export class BaseConfig extends Entity {
@property({
type: 'object',
required: true,
})
configMetadata: ConfigMetadata;
@property({
type: 'array',
itemType: 'object',
})
sharedUsers: Array<SharedUsers>;
@property({
type: 'array',
itemType: 'object',
})
sharedRoles: Array<SharedRoles>;
constructor(data?: Partial<BaseConfig>) {
super(data);
}
}
export interface BaseConfigRelations {
// describe navigational properties here
}
export type BaseConfigWithRelations = BaseConfig & BaseConfigRelations;
我的configmetadata模型看起来像这样:
@model({ settings: { strict: true } })
export class ConfigMetadata extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
required: true,
})
description: string;
@property({
type: 'date',
required: true,
})
dateCreated: string;
@property({
type: 'date',
required: true,
})
lastUpdatedOn: string;
@property({
type: 'string',
required: true,
})
creatorId: string;
@property({
type: 'string',
required: true,
})
creatorName: string;
constructor(data?: Partial<ConfigMetadata>) {
super(data);
}
}
....
我的控制器中有一个帖子端点,它使用request body,使用getmodelschemaref(myobj)
@post('/product-view-configs')
@response(200, {
description: 'ProductViewConfig model instance',
content: { 'application/json': { schema: getModelSchemaRef(ProductViewConfig) } },
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(ProductViewConfig,
{
title: 'NewProductViewConfig',
}),
},
},
})
productViewConfig: ProductViewConfig,
): Promise<ProductViewConfig> {
return this.productViewConfigRepository.create(productViewConfig);
}
,这是我实际问题:
为什么请求主体看起来像这样?
https://i.sstatic.net/jlm93.png“ alt =”在此处输入图像描述”>
当我真正期望的是一个在请求主体中看起来像这样的对象:
{
"_id" : "string",
"configMetadata" : {
"name" : "string",
"description" : "string",
"createdOn" : "date",
"lastUpdatedBy" : "date",
"creatorId" : "string",
"creatorName" : "string"
},
"sharedUsers" : [
{...}
],
"sharedRoles" : [
{...}
],
"tiles" : [
{...}
]
}
那么为什么不这些属性来自Baseconfig?有人可以向我指出正确的方向吗?我无法从loopback4文档中弄清楚这一点,因此对任何帮助都将不胜感激!!
Could someone help me understand how models in loopback4 work?
I defined a model that looks like this:
@model()
export class ProductViewConfig extends BaseConfig {
@property({
type: 'string',
id: true,
generated: true,
})
_id?: string;
@property({
type: 'array',
itemType: 'object',
})
tiles: Array<TileOptions>;
constructor(data?: Partial<ProductViewConfig>) {
super(data);
}
}
export interface ProductViewConfigRelations {
// describe navigational properties here
}
export type ProductViewConfigWithRelations = ProductViewConfig & ProductViewConfigRelations;
the baseConfig class that it extends from looks like this:
@model({
settings: {
strict: true
}
})
export class BaseConfig extends Entity {
@property({
type: 'object',
required: true,
})
configMetadata: ConfigMetadata;
@property({
type: 'array',
itemType: 'object',
})
sharedUsers: Array<SharedUsers>;
@property({
type: 'array',
itemType: 'object',
})
sharedRoles: Array<SharedRoles>;
constructor(data?: Partial<BaseConfig>) {
super(data);
}
}
export interface BaseConfigRelations {
// describe navigational properties here
}
export type BaseConfigWithRelations = BaseConfig & BaseConfigRelations;
and My ConfigMetadata Model looks like this:
@model({ settings: { strict: true } })
export class ConfigMetadata extends Entity {
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
required: true,
})
description: string;
@property({
type: 'date',
required: true,
})
dateCreated: string;
@property({
type: 'date',
required: true,
})
lastUpdatedOn: string;
@property({
type: 'string',
required: true,
})
creatorId: string;
@property({
type: 'string',
required: true,
})
creatorName: string;
constructor(data?: Partial<ConfigMetadata>) {
super(data);
}
}
....
And I have a post endpoint in my controller with request body that uses getModelSchemaRef(myObj)
@post('/product-view-configs')
@response(200, {
description: 'ProductViewConfig model instance',
content: { 'application/json': { schema: getModelSchemaRef(ProductViewConfig) } },
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(ProductViewConfig,
{
title: 'NewProductViewConfig',
}),
},
},
})
productViewConfig: ProductViewConfig,
): Promise<ProductViewConfig> {
return this.productViewConfigRepository.create(productViewConfig);
}
and here is my actual question:
why does the request body look like this?
when Really what I expect is an an Object that looks like this in the request body:
{
"_id" : "string",
"configMetadata" : {
"name" : "string",
"description" : "string",
"createdOn" : "date",
"lastUpdatedBy" : "date",
"creatorId" : "string",
"creatorName" : "string"
},
"sharedUsers" : [
{...}
],
"sharedRoles" : [
{...}
],
"tiles" : [
{...}
]
}
so why don't these properties from baseConfig appear? Could someone please point me in the right direction? I couldn't figure it out from the loopback4 documentation so any help would be greatly appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
关系
将模型关联。Hasmany
,hasone
等。回环中的主题可用于开始研究。https://loopback.io/doc/doc/en/lb4/relations.html
You should use
relations
for associate the models.hasMany
,hasOne
etc. topics in the loopback can be usefull for starting research.https://loopback.io/doc/en/lb4/Relations.html