typescript 如何使用bulkCreate Sequelize?

xvw2m8pv  于 2023-03-31  发布在  TypeScript
关注(0)|答案(1)|浏览(214)

我想用bulkCreate函数将数组中的数据发送到数据库。为此,我需要发送数据和userId。
数据示例:

{
    description: 'Pasta',
    creator: 'Andre',
    messageHelper: '',
    startDatetime: '2023-04-08T11:00:00',
    endDatetime: '2023-04-08T11:30:00',
    type: 'todo',
    groupId: 21
  },
  {
    description: 'Pasta',
    creator: 'Andre',
    messageHelper: '',
    startDatetime: '2023-04-15T11:00:00',
    endDatetime: '2023-04-15T11:30:00',
    type: 'todo',
    groupId: 21
  },
  {
    description: 'Pasta',
    creator: 'Andre',
    messageHelper: '',
    startDatetime: '2023-04-22T11:00:00',
    endDatetime: '2023-04-22T11:30:00',
    type: 'todo',
    groupId: 21
  },

requests.controller.ts:

@UseGuards(AuthGuard('jwt'))
    @Post('recurrent')
    async createRecurrent(@Body() request, @Request() req): Promise<RequestEntity[]> {
        return await this.requestService.createRecurrent(request, req.user.id);
    }

requests.service.ts(带bulkCreate):

async createRecurrent(request: RequestDto[], userId): Promise<Request[]> {   
        return await this.requestRepository.bulkCreate<Request>({ ...request, userId });
    }

/*error on 'userId' : Argument of type '{ userId: any; length: number; toString(): string; toLocaleString(): string; pop(): RequestDto; push(...items: RequestDto[]): number; concat(...items: ConcatArray<RequestDto>[]): RequestDto[]; concat(...items: (RequestDto | ConcatArray<...>)[]): RequestDto[]; ... 25 more ...; [Symbol.unscopables](): { ...; }; }' is not assignable to parameter of type 'object[]'.
Object literal may only specify known properties, and 'userId' does not exist in type 'object[]'*/

requests.entity.ts:

@Table
export class Request extends Model<Request> {
    @AllowNull
    @Column({
        type: DataType.STRING,
    })
    description: string;

    @Column({
        type: DataType.STRING,
        allowNull: false,
    })
    creator: string;

    @Column({
        type: DataType.STRING,
        allowNull: true,
    })
    messageHelper: string;

    @AllowNull
    @Column
    service: string

    @Column({
        type: DataType.DATE,
        allowNull: false,
    })
    startDatetime: Date;

    @Column({
        type: DataType.DATE,
        allowNull: false,
    })
    endDatetime: Date;
    @Column({
        type: DataType.BOOLEAN,
        allowNull: false,
        defaultValue: 0,
    })
    isFree: number;
    
    @Column({
        type: DataType.ENUM,
        values: ['todo','meal'],
        allowNull: false,
    })
    type: string;

    @ForeignKey(() => Group)
    @Column
    groupId: number;

    @BelongsTo(() => Group)
    group: Group;
}

requestDto.ts:

enum RequestType {
    MEAL = 'meal',
    TODO = 'todo'
}

export class RequestDto {

    @IsOptional()
    readonly description: string;

    @IsNotEmpty()
    readonly creator: string;

    @IsOptional()
    readonly messageHelper: string;

    @IsOptional()
    readonly service: string;

    @IsNotEmpty()
    readonly startDatetime: Date;

    @IsNotEmpty()
    readonly endDatetime: Date;

    @IsNotEmpty()
    @IsEnum(RequestType, {
        message: 'type must be either meal or todo',
    })
    readonly type: RequestType;

    readonly isFree: number;

    readonly groupId: number;
}

需要userId将表连接到另一个表,所以我不能删除它。
我认为,因为userId不是一个数组,它抛出错误requests. service. ts中存在。我试图将其更改为数组,但同样的错误。我真的不明白错误,并在互联网上搜索并没有真正帮助我。
希望有人能帮助我!提前感谢!

dgiusagp

dgiusagp1#

只需将您的服务替换为:

request.forEach(d => d['userId'] = userId)
return await this.requestRepository.bulkCreate<Request>(request);

这将把你的userId插入到每个对象中,这样你就可以做出正确的关节。

相关问题