fix: handle null response from records API to prevent product list crash

When products have no inventory records, the backend returns {"code":0,"data":null}.
getRecords() resolved with null, causing records.filter() to throw TypeError,
which crashed the entire fetchData() and left the product list empty.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 22:11:09 +08:00
parent 29451277d1
commit b24de9ac11
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0

View File

@ -63,17 +63,17 @@ export function useInventoryData({ companyId, activeType }: UseInventoryDataOpti
try {
const productsRes = await purchaserApi.listProducts();
const products: Product[] = productsRes.data;
const products: Product[] = productsRes.data || [];
const recordResults = await Promise.all(
products.map(p =>
purchaserApi.getRecords(p.id).catch(() => [] as ProductInRecord[])
purchaserApi.getRecords(p.id).then(r => r || []).catch(() => [] as ProductInRecord[])
)
);
const allBatches: FabricBatch[] = [];
const updatedProducts: ProductWithInventory[] = products.map((p, i) => {
const records = recordResults[i];
const records = recordResults[i] || [];
const inRecords = records.filter(r => r.record_type !== 'out');
const outRecords = records.filter(r => r.record_type === 'out');