async findAll(queryParams: IItemQueryParams): Promise<ItemEntity[]> { const query:…
- User
async findAll(queryParams: IItemQueryParams): Promise<ItemEntity[]> { const query: FindManyOptions = { where: { status: ItemStatus.REMOVED, }, relations: ['category', 'events', 'location'], order: { cost: queryParams.sortByCost }, };
if (queryParams.eventId) { query.where['events.id'] = queryParams.eventId; } if (queryParams.regoNumber) { query.where.regoNumber = queryParams.regoNumber; } if (queryParams.name) { query.where.name = queryParams.name; } if (queryParams.charityList) { query.where.charityList = queryParams.charityList; } if (queryParams.notes) { query.where.notes = queryParams.notes; } if (queryParams.provenance) { query.where.provenance = queryParams.provenance; } if (queryParams.category) { query.where['category.name'] = queryParams.category; } if (queryParams.subcategory) { query.where['category.subCategories'] = queryParams.subcategory; } if (queryParams.status) { query.where.status = queryParams.status; } if (queryParams.costFrom) { query.where.cost = query.where.cost || {}; query.where.cost.$gte = queryParams.costFrom; } if (queryParams.costTo) { query.where.cost = query.where.cost || {}; query.where.cost.$lte = queryParams.costTo; } if (queryParams.locationId) { query.where['location.id'] = queryParams.locationId; } return this.itemRepository.find(query); } i keep getting the following errors when i run the above code ^TSError: ⨯ Unable to compile TypeScript: src/modules/items/item.service.ts:166:25 - error TS2339: Property 'regoNumber' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'regoNumber' does not exist on type 'FindOptionsWhere[]'.
166 query.where.regoNumber = queryParams.regoNumber; ~~~~~~~~~~ src/modules/items/item.service.ts:170:25 - error TS2339: Property 'name' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'name' does not exist on type 'FindOptionsWhere[]'.
170 query.where.name = queryParams.name; ~~~~ src/modules/items/item.service.ts:174:25 - error TS2339: Property 'charityList' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'charityList' does not exist on type 'FindOptionsWhere[]'.
174 query.where.charityList = queryParams.charityList; ~~~~~~~~~~~ src/modules/items/item.service.ts:178:25 - error TS2339: Property 'notes' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'notes' does not exist on type 'FindOptionsWhere[]'.
178 query.where.notes = queryParams.notes; ~~~~~ src/modules/items/item.service.ts:182:25 - error TS2339: Property 'provenance' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'provenance' does not exist on type 'FindOptionsWhere[]'.
182 query.where.provenance = queryParams.provenance; ~~~~~~~~~~ src/modules/items/item.service.ts:194:25 - error TS2339: Property 'status' does not exist on type 'FindOptionsWhere | FindOptionsWhere[]'. Property 'status' does not exist on type 'FindOptionsWhere[]'.
- Assistant
The error message suggests that some of the properties you are trying to access on
query.wheredo not exist on theFindOptionsWheretype.One way to fix this is to add type assertions to tell TypeScript that
query.whereis of typePartial<ItemEntity>, like this:if (queryParams.regoNumber) { (query.where as Partial<ItemEntity>).regoNumber = queryParams.regoNumber; } if (queryParams.name) { (query.where as Partial<ItemEntity>).name = queryParams.name; } if (queryParams.charityList) { (query.where as Partial<ItemEntity>).charityList = queryParams.charityList; } // ...and so onAlternatively, you can disable type checking for
query.whereby casting it toany, like this:(query as any).where.regoNumber = queryParams.regoNumber; (query as any).where.name = queryParams.name; (query as any).where.charityList = queryParams.charityList; // ...and so onHowever, these solutions may suppress type errors that could indicate other problems in your code, so it's better to try and fix the root cause of the issue. One way to do this is to make sure that the type is correctly defined and that the properties on the interface match the properties on the type.