درود استاد
استاد برای نمایش محصولات دسته بندی،اسلاگ رو از سمت فرانت فرستادیم اینجا میگیریم و بر اساس آیدی اون کتگوری میخواهیم محصولاتی که اون categoryرو دارن از repository مربوط به product پیدا کنیم.
در کنترلر فرانت مربوط به کتگوری داریم:
public async products(req: Request, res: Response, next: NextFunction) {
const { slug } = req.params
const category = this.repository.findBySlug(slug)
if (!category) {
throw new NotFoundException('دسته بندی مورد نظر یافت نشد')
}
const productRepository = new ProductMongoRepository()
const products = await productRepository.findMany({ category: category.id })
res.send({
success: true
products
})
}
از Types مونگوس استفاده میکنیم.و در productMongoRepository آبجکت آیدی مونگوس رو میسازیم.categoryرو بهش پاس میدیم.بعد کوئری رو میسازیم و فایند میکنیم:
public async findMany(
params: ObjectInterface,
relations?: string[],
pagination?: IPagination,
sort?: any
): Promise<IProduct[]> {
const productQueryParams: ObjectInterface = {}
if (params.category) {
const ObjectID = Types.ObjectId
productQueryParams.category = new ObjectID(params.category)
}
const productQuery = ProductModel.find(productQueryParams)
:خطایی که تو کنسول بک گراند نشون میده
TSError: ⨯ Unable to compile TypeScript:
src/components/category/front/CategoriesController.ts:36:76 - error TS2339: Property 'id' does not exist on type 'Promise<ICategory | null>'.
36 const products = await productRepository.findMany({ category: category.id })
~~
src/components/category/front/CategoriesController.ts:39:7 - error TS1005: ',' expected.
39 products
~~~~~~~~
:Error serializing خطایی که در سمت فرانت نشون میده همون
Server Error
Error: Error serializing `.category` returned from `getStaticProps` in "/category/[slug]".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
isSerializable