typescript 删除实体数组- postgres & nestjs with sequelize

8ehkhllq  于 2023-08-07  发布在  TypeScript
关注(0)|答案(1)|浏览(120)

这是我的create函数:

async createDocument(createDocumentDto: CreateDocumentDto) {
    const request = await Document.build(<any>createDocumentDto);
    return await request.save();
  }

字符串
现在我试图删除类型CreateDocumentDto[]的数组,但我没有从文档中获得类似'bulkDelete'的内容
我试着去做:

const entities = await this.AnswersRepository.findAll(<any>deleteAnswerDto);
    if (!entities) {
      throw new NotFoundException(`Something went wrong, no changes applied!`);
    }
    return this.AnswersRepository.remove(entities);


但我得到了这个错误:“类型”typeof Answer“上不存在属性”remove "
我找不到解决的办法。会感激帮助:)。

g0czyy6m

g0czyy6m1#

在你的NestJS应用中,不要忘记配置路由并适当地处理请求。使用Sequelize和NestJS,这段代码解释了如何批量删除文档。
在DocumentService中:

@Injectable()
export class DocumentService {
  async createDocument(createDocumentDto: CreateDocumentDto) {
    const document = await Document.build(createDocumentDto);
    return await document.save();
  }

  async deleteDocuments(documentIds: number[]) {
    return Document.destroy({
      where: {
        id: documentIds,
      },
    });
  }
}

字符串
在DocumentController中:

@Controller('documents')
export class DocumentController {
  constructor(private readonly documentService: DocumentService) {}

  @Delete()
  async deleteDocuments(@Body() documentIds: number[]) {
    await this.documentService.deleteDocuments(documentIds);
    return 'Documents deleted';
  }
}

相关问题