diff --git a/backend/src/api/dailyInventory.js b/backend/src/api/dailyInventory.js index 45a3dc1..aec520f 100644 --- a/backend/src/api/dailyInventory.js +++ b/backend/src/api/dailyInventory.js @@ -283,7 +283,7 @@ router.get('/', async (req, res) => { // 流水列:自期初时刻(最新业务日 01:00)以来的单据合计(带符号,流出为负) let flowMap = new Map(); if (openingDate) { - flowMap = await fetchFlows(wmsCoIds, `${latestBizDate} 01:00:00`).catch((e) => { + flowMap = await fetchFlows(cloudDb, wmsCoIds, `${latestBizDate} 01:00:00`).catch((e) => { console.error('[daily-inventory] 流水取数失败:', e.message); return new Map(); }); diff --git a/backend/src/services/dailyInventoryFlows.js b/backend/src/services/dailyInventoryFlows.js index 41014aa..0c612d3 100644 --- a/backend/src/services/dailyInventoryFlows.js +++ b/backend/src/services/dailyInventoryFlows.js @@ -93,6 +93,9 @@ const AVAILABLE_FLOW_FIELDS = FLOW_QUERIES.map((q) => q.field); * @returns {Promise>} key = wmsCoId 字符串,value = { purchaseIn, purchaseReturn, salesOut, salesReturn }(带符号,无记录的字段不出现) */ async function fetchFlows(cloudDb, wmsCoIds, fromText) { + if (!cloudDb || typeof cloudDb.query !== 'function') { + throw new TypeError('fetchFlows 第一个参数必须是数据库实例(防呆:调用漏传 cloudDb 时立即报错而非静默返回空)'); + } const map = new Map(); if (!wmsCoIds.length || !fromText) return map; for (const q of FLOW_QUERIES) { diff --git a/backend/test/dailyInventoryFlows.test.js b/backend/test/dailyInventoryFlows.test.js index d58efed..23e1d66 100644 --- a/backend/test/dailyInventoryFlows.test.js +++ b/backend/test/dailyInventoryFlows.test.js @@ -105,3 +105,10 @@ test('deriveAndStoreOpeningSnapshot:监控表命中走精确值,未命中用 // 带回基线仓库名,保证后续按 wmsCoId 读取正确 assert.equal(byWh.get(102).warehouse, '仓B主仓'); }); + +test('fetchFlows 缺少数据库实例参数时立即抛错(防静默空结果)', async () => { + await assert.rejects( + () => fetchFlows([101, 102], '2026-09-26 01:00:00'), + /第一个参数必须是数据库实例/ + ); +});