Loopback POST 条目数组?

Loopback POST array of entry?

我想插入 10 个条目,其中一个查询针对 10 个查询。

我读到可以通过发送这样的数组来做到这一点:

但是我得到这个错误:

我需要设置什么吗?完全不知道怎么办。

回购样本:https://github.com/mathias22osterhagen22/loopback-array-post-sample

编辑: 人-model.ts:

import {Entity, model, property} from '@loopback/repository';

@model()
export class People extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  id?: number;

  @property({
    type: 'string',
    required: true,
  })
  name: string;


  constructor(data?: Partial<People>) {
    super(data);
  }
}

export interface PeopleRelations {
  // describe navigational properties here
}

export type PeopleWithRelations = People & PeopleRelations;

您的代码存在的问题是:

"name": "ValidationError", "message": "The People instance is not valid. Details: 0 is not defined in the model (value: undefined); 1 is not defined in the model (value: undefined); name can't be blank (value: undefined).",

在上面的@requestBody 模式中,您正在申请插入单个对象 属性,而在您的正文中,您正在发送 [people] 对象的数组。

正如您在 people.model.ts 中看到的那样,您已声明需要 属性 名称,因此系统会找到 属性 "name",这显然在给定的对象数组作为主节点。

因为你正在传递索引数组,所以很明显的错误是你没有任何 属性 命名为 0 或 1,所以它会抛出错误。

以下是您应该应用的代码帽,以插入该类型的多个项目。

@post('/peoples', {
 responses: {
    '200': {
      description: 'People model instance',
      content: {
        'application/json': {
          schema: getModelSchemaRef(People)
        }
      },
    },
  },
})
async create(
  @requestBody({
    content: {
      'application/json': {
        schema: {
          type: 'array',
          items: getModelSchemaRef(People, {
            title: 'NewPeople',
            exclude: ['id'],
          }),
        }
      },
    },
  })
  people: [Omit<People, 'id'>]
): Promise<{}> {
  people.forEach(item => this.peopleRepository.create(item))
  return people;
}

您也可以使用下面的

Promise<People[]> {
  return await this.peopleRepository.createAll(people)
}

您可以通过修改请求来传递您的人物模型数组body.If您需要更多帮助可以发表评论。 我想你现在有一个明确的解决方案。 "Happy Loopbacking :)"