Skip to content

Commit dfdacb4

Browse files
committed
fix: empty array handling
1 parent eedb4a4 commit dfdacb4

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

src/app/api/products/route.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ async function getProductsByCollectionId(queryParams: Record<string, any>) {
5454

5555
const products = data.map((c) => c.products).flat() as ProductDTO[]
5656

57-
if (!products) return []
58-
5957
const publishedProducts = filterPublishedProducts(products)
6058

6159
const count = publishedProducts.length
@@ -78,6 +76,7 @@ async function getProductsByCollectionId(queryParams: Record<string, any>) {
7876

7977
async function getProducts(params: Record<string, any>) {
8078
const productService = await initializeProductModule()
79+
8180
const { id, limit, offset, cart_id } = params
8281

8382
const filters = {} as FilterableProductProps
@@ -93,10 +92,6 @@ async function getProducts(params: Record<string, any>) {
9392
withDeleted: false,
9493
})
9594

96-
if (!data) {
97-
return []
98-
}
99-
10095
const publishedProducts = filterPublishedProducts(data)
10196

10297
const productsWithPrices = await getPrices(publishedProducts, cart_id)

src/lib/util/get-product-prices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { ProductDTO, ProductVariantDTO } from "@medusajs/types/dist/product"
66
* @param data Array of product objects (with variants) to get prices for
77
* @param cartId (Optional) cart id to get region-specific prices
88
*/
9-
export default async function getVariants(data: ProductDTO[], cartId?: string) {
9+
export default async function getPrices(data: ProductDTO[], cartId?: string) {
1010
if (!data || !data.length) {
11-
throw new Error("No products provided")
11+
return []
1212
}
1313

1414
// Map of variant id to variant object

0 commit comments

Comments
 (0)