Sequelize Typescript:类型 'FavoriteEntity[]' 不可分配给类型 'FavoriteEntity | null'
Sequelize Typescript: Type 'FavoriteEntity[]' is not assignable to type 'FavoriteEntity | null'
我正在使用 Sequelize-Typescript 作为 ORM 编写 NestJS 应用程序。
我在这里尝试获取用户最喜欢的工作 (M:M),因此我有一个 UserEntity、一个 JobEntity(与此问题无关)和一个 FavoriteEntity。
favorite.entity.ts
import { Table, Column, Model, PrimaryKey, ForeignKey, BelongsTo, NotNull } from "sequelize-typescript";
import { IDefineOptions } from "sequelize-typescript/lib/interfaces/IDefineOptions";
import { UserEntity } from "../users/user.entity";
import { JobEntity } from "../jobs/job.entity";
const tableOptions: IDefineOptions = {
timestamp: true,
tableName: "favorites",
schema: process.env.DB_SCHEMA,
} as IDefineOptions;
@Table(tableOptions)
export class FavoriteEntity extends Model<FavoriteEntity> {
@BelongsTo(() => UserEntity)
user: UserEntity;
@ForeignKey(() => UserEntity)
@PrimaryKey
@NotNull
@Column
userId: number;
@BelongsTo(() => JobEntity)
job: JobEntity;
@ForeignKey(() => JobEntity)
@PrimaryKey
@NotNull
@Column
jobId: number;
}
favorite.service.ts
import { Inject, Injectable } from "@nestjs/common";
import { Model } from "sequelize-typescript";
import { IFavorite, IFavoriteService } from "./interfaces";
import { FavoriteEntity } from "./favorite.entity";
@Injectable()
export class FavoriteService implements IFavoriteService {
constructor(
@Inject("FavoriteRepository") private readonly favoriteRepository: typeof Model,
@Inject("SequelizeInstance") private readonly sequelizeInstance,
) {}
public async findByUserId(userId: number): Promise<FavoriteEntity | null> {
return await FavoriteEntity.scope().findAll<FavoriteEntity>({
where: { userId },
});
}
}
我收到一个我真的不明白的类型错误:
TSError: ⨯ Unable to compile TypeScript:
src/modules/favorite/favorite.service.ts(21,5): error TS2322: Type
'FavoriteEntity[]' is not assignable to type 'FavoriteEntity | null'.
Type 'FavoriteEntity[]' is not assignable to type 'FavoriteEntity'.
Property 'user' is missing in type 'FavoriteEntity[]'.
我不明白为什么它抱怨缺少 user
,它显然在那里。 (如果我在实体中将其设为可选(?),它会抱怨下一个 属性 直到我将它们全部设为可选,然后它会以相同的方式抱怨缺少 属性 dataValues)
我错过了什么?
谢谢!
更新:
所以我可能已经想通了一些东西。如果我这样写
return await FavoriteEntity.scope().findAll<FavoriteEntity[]>({
where: { userId },
});
用 FavoriteEntity[]
而不是 FavoriteEntity
,我得到
Property 'dataValues' is missing in type 'FavoriteEntity[]'
我真的不知道哪个是正确的写法,但我仍然有问题...
findAll
将 return 实体数组。您只想要一个或 null。为此,请使用 findOne
。此外,您不需要在此处执行 return await
。除非在 try-catch 块中,否则这是多余的。
我正在使用 Sequelize-Typescript 作为 ORM 编写 NestJS 应用程序。
我在这里尝试获取用户最喜欢的工作 (M:M),因此我有一个 UserEntity、一个 JobEntity(与此问题无关)和一个 FavoriteEntity。
favorite.entity.ts
import { Table, Column, Model, PrimaryKey, ForeignKey, BelongsTo, NotNull } from "sequelize-typescript";
import { IDefineOptions } from "sequelize-typescript/lib/interfaces/IDefineOptions";
import { UserEntity } from "../users/user.entity";
import { JobEntity } from "../jobs/job.entity";
const tableOptions: IDefineOptions = {
timestamp: true,
tableName: "favorites",
schema: process.env.DB_SCHEMA,
} as IDefineOptions;
@Table(tableOptions)
export class FavoriteEntity extends Model<FavoriteEntity> {
@BelongsTo(() => UserEntity)
user: UserEntity;
@ForeignKey(() => UserEntity)
@PrimaryKey
@NotNull
@Column
userId: number;
@BelongsTo(() => JobEntity)
job: JobEntity;
@ForeignKey(() => JobEntity)
@PrimaryKey
@NotNull
@Column
jobId: number;
}
favorite.service.ts
import { Inject, Injectable } from "@nestjs/common";
import { Model } from "sequelize-typescript";
import { IFavorite, IFavoriteService } from "./interfaces";
import { FavoriteEntity } from "./favorite.entity";
@Injectable()
export class FavoriteService implements IFavoriteService {
constructor(
@Inject("FavoriteRepository") private readonly favoriteRepository: typeof Model,
@Inject("SequelizeInstance") private readonly sequelizeInstance,
) {}
public async findByUserId(userId: number): Promise<FavoriteEntity | null> {
return await FavoriteEntity.scope().findAll<FavoriteEntity>({
where: { userId },
});
}
}
我收到一个我真的不明白的类型错误:
TSError: ⨯ Unable to compile TypeScript: src/modules/favorite/favorite.service.ts(21,5): error TS2322: Type 'FavoriteEntity[]' is not assignable to type 'FavoriteEntity | null'. Type 'FavoriteEntity[]' is not assignable to type 'FavoriteEntity'. Property 'user' is missing in type 'FavoriteEntity[]'.
我不明白为什么它抱怨缺少 user
,它显然在那里。 (如果我在实体中将其设为可选(?),它会抱怨下一个 属性 直到我将它们全部设为可选,然后它会以相同的方式抱怨缺少 属性 dataValues)
我错过了什么?
谢谢!
更新: 所以我可能已经想通了一些东西。如果我这样写
return await FavoriteEntity.scope().findAll<FavoriteEntity[]>({
where: { userId },
});
用 FavoriteEntity[]
而不是 FavoriteEntity
,我得到
Property 'dataValues' is missing in type 'FavoriteEntity[]'
我真的不知道哪个是正确的写法,但我仍然有问题...
findAll
将 return 实体数组。您只想要一个或 null。为此,请使用 findOne
。此外,您不需要在此处执行 return await
。除非在 try-catch 块中,否则这是多余的。