请求正文模式缺少属性/为什么我得到不完整的模式模型

发布于 2025-01-18 09:32:39 字数 3313 浏览 5 评论 0原文

有人可以帮助我了解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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2025-01-25 09:32:39

您应该使用关系将模型关联。 Hasmanyhasone等。回环中的主题可用于开始研究。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文