为啥我得到“UnhandledPromiseRejectionWarning:错误:未定义类型错误。确保提供显式类型”即使在我指定了类型之后?
Posted
技术标签:
【中文标题】为啥我得到“UnhandledPromiseRejectionWarning:错误:未定义类型错误。确保提供显式类型”即使在我指定了类型之后?【英文标题】:Why I get "UnhandledPromiseRejectionWarning: Error: Undefined type error. Make sure are providing an explicit type" even after I have specified type?为什么我得到“UnhandledPromiseRejectionWarning:错误:未定义类型错误。确保提供显式类型”即使在我指定了类型之后? 【发布时间】:2021-09-09 07:35:20 【问题描述】:create 方法运行良好。没有错误。但我需要一种方法来一次插入大量记录。 这就是我尝试使用 createQueryBuilder 的原因。
它说未定义类型错误的原因是什么?我已经将类型定义为布尔值。
这是我的解析器
@Resolver(() => Vehicle)
export class VehicleResolver
constructor(private vehicleService: VehicleService)
@Mutation(() => Vehicle, name: 'createVehicle' )
create(@Args('vehicleInput') vehicle: VehicleCreateDTO)
return this.vehicleService.create(vehicle);
@Mutation(() => Boolean, name: 'createVehicleBulk' )
async createVehicleBulk(@Args('vehiclesInput') vehicles: [VehicleCreateDTO])
const res = await this.vehicleService
.createBulk(vehicles)
.catch((err) => console.log(err));
if (res) return true;
else return false;
这是我的服务
@Injectable()
export class VehicleService
constructor(
@InjectRepository(Vehicle) private vehicleRepo: Repository<Vehicle>,
)
create(vehicle: VehicleCreateDTO): Promise<Vehicle>
const vcl = this.vehicleRepo.create(vehicle);
return this.vehicleRepo.save(vcl);
async createBulk(vehicles: VehicleCreateDTO[]): Promise<InsertResult>
const res = await this.vehicleRepo
.createQueryBuilder()
.insert()
.into('vehicle')
.values(vehicles)
.execute();
return res;
我想保存一个 VehicleCreateDTO 类型的对象数组。但我收到以下错误。
(node:11464) UnhandledPromiseRejectionWarning: Error: Undefined type error. Make sure you are providing an explicit type for the "createBulk" (parameter at index [0]) of the "VehicleResolver" class.
at Object.reflectTypeFromMetadata (E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\dist\utils\reflection.utilts.js:15:15)
at E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\dist\decorators\args.decorator.js:17:78
at E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\dist\schema-builder\storages\lazy-metadata.storage.js:36:44
at Array.forEach (<anonymous>)
at LazyMetadataStorageHost.load (E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\dist\schema-builder\storages\lazy-metadata.storage.js:36:26)
at GraphQLSchemaFactory.<anonymous> (E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\dist\schema-builder\graphql-schema.factory.js:37:57)
at Generator.next (<anonymous>)
at E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\node_modules\tslib\tslib.js:117:75
at new Promise (<anonymous>)
at Object.__awaiter (E:\COL\backend\vehicle-service-insert\node_modules\@nestjs\graphql\node_modules\tslib\tslib.js:113:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11464) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:11464) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
【问题讨论】:
您找到解决方案了吗? 【参考方案1】:您的输入类型参数的符号似乎是错误的。此外,如果我仔细观察,错误消息似乎具有误导性
...类型为“createBulk”(索引 [0] 处的参数) “VehicleResolver”类...
但是在您的解析器中,您只有 createVehicle
和 createVehicleBulk
的突变,所以我认为错误是 createVehicleBulk
突变。
只是改变:
@Mutation(() => Boolean, name: 'createVehicleBulk' )
async createVehicleBulk(@Args('vehiclesInput') vehicles: [VehicleCreateDTO])
const res = await this.vehicleService
.createBulk(vehicles)
.catch((err) => console.log(err));
if (res) return true;
else return false;
到
@Mutation(() => Boolean, name: 'createVehicleBulk' )
async createVehicleBulk(@Args( name: 'vehiclesInput', type: () => [VehicleCreateDTO]) vehicles: VehicleCreateDTO[])
const res = await this.vehicleService
.createBulk(vehicles)
.catch((err) => console.log(err));
if (res) return true;
else return false;
我遇到了类似的问题,answer 帮助我找到了解决方案
【讨论】:
以上是关于为啥我得到“UnhandledPromiseRejectionWarning:错误:未定义类型错误。确保提供显式类型”即使在我指定了类型之后?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我得到一个 nullPointerException? [复制]