基于Node.js+Express的REST API调用MariaDB时出现solarData.getMultiple is not a function错误求助
Fixing "solarData.getMultiple is not a function" Error
Hey there! Let's break down why you're seeing this error and how to fix it quickly.
The Root Cause
The error happens because you've defined the getMultiple function in ./services/solarData.js, but you haven't exported it. When your route file (./routes/solarData.js) imports solarData, it's getting an empty object instead of the function you wrote.
The Fix
Add an export statement at the very end of ./services/solarData.js to make the getMultiple function available to other files:
const db = require('./db'); const helper = require('../helper'); const config = require('../config'); async function getMultiple(page = 1){ const offset = helper.getOffset(page, config.listPerPage); const rows = await db.query( `SELECT idxDTP, year(DT) AS Year, month(DT) AS Month, day(DT) AS Day, hour(DT) AS Hour, minute(DT) AS Min, PWR AS Power FROM DTP WHERE (DT >= '2021-01-12 12:30:00') AND (DT <= '2021-01-12 12:40:00') ORDER BY Year ASC, Month ASC, Day ASC, Hour ASC, Min ASC LIMIT ?,?`, [offset, config.listPerPage] ); const data = helper.emptyOrRows(rows); const meta = {page}; return { data, meta } } // Add this line to export the function module.exports = { getMultiple };
Additional Checks to Ensure Everything Works
- Verify File Paths: Double-check that
require('../services/solarData')in your route file points to the correct location ofsolarData.js. A wrong relative path can also lead to importing an empty module. - Restart Your Server: After making this change, stop and restart your Node.js server to ensure the updated module is loaded.
- Check Helper Functions: Make sure your
helper.jsfile (which you import insolarData.js) has thegetOffsetandemptyOrRowsfunctions defined correctly—this will prevent future errors once the export issue is fixed.
Once you apply the export fix, your /solarData endpoint should be able to call getMultiple successfully and return the database data as JSON.
内容的提问来源于stack exchange,提问作者Greg
相关产品推荐
相关产品推荐

