节点异步函数 returns 'is not a function'
Node Async function returns 'is not a function'
我一直在关注 tutorial 在 Node 中设置 REST API,使用 Express 访问现有 MariaDB 数据库的应用程序。我的版本只需要读取数据,并且我将 DB 与 Node 应用程序放在一起(同一主机)。
对于这个入门级示例,我的目标是仅使用静态 SQL 访问数据,因此我可以看到它由 JSON pritifier 在网页中呈现。
[接下来,我想在table(EJS?)中呈现数据。稍后,当我可以让它工作时,我将添加表单控件(React?)让用户指定 SQL 查询的开始和结束日期范围。最后,我的目标是将数据呈现为折线图 (D3js)。]
本教程成功运行了 Web 服务器(它 returns 'OK' 在基础 URL 上),但是当我转到 URL/solarData
时,它尝试使用异步函数来从数据库中获取多行,它响应 solarData.getMultiple is not a function TypeError: solarData.getMultiple is not a function
./app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 3800;
const solarDataRouter = require('./routes/solarData');
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
app.get('/', (req, res) => {
res.json({'message': 'ok'});
})
app.use('/solarData', solarDataRouter);
/* Error handler middleware */
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
console.error(err.message, err.stack);
res.status(statusCode).json({'message': err.message});
return;
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
./routes/solarData.js
const express = require('express');
const router = express.Router();
const solarData = require('../services/solarData');
/* GET solar data. */
router.get('/', async function(req, res, next) {
try {
res.json(await solarData.getMultiple(req.query.page));
} catch (err) {
console.error(`Error while getting solar data `, err.message);
next(err);
}
});
module.exports = router;
./config.js
const env = process.env;
const config = {
db: {
host: env.SUNNY_HOST,
user: env.SUNNY_USER,
password: env.SUNNY_PW,
database: env.SUNNY_DB,
},
listPerPage: env.LIST_PER_PAGE,
};
module.exports = config;
./services/solarData.js
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
}
}
./services/db.js
const mysql = require('mysql2/promise');
const config = require('../config');
async function query(sql, params) {
const connection = await mysql.createConnection(config.db);
const [results, ] = await connection.execute(sql, params);
return results;
}
module.exports = {
query
}
我遗漏了./helper.js
一切正常,直到我将网页指向 /solarData。那时我得到:
调试控制台 (vscode)
/Error while getting solar data solarData.getMultiple is not a function
routes/solarData.js:10
solarData.getMultiple is not a function TypeError: solarData.getMultiple is not a function
at /SunnyData/solarViz/routes/solarData.js:8:30
at Layer.handle [as handle_request] (/SunnyData/solarViz/node_modules/express/lib/router/layer.js:95:5)
at next (/SunnyData/solarViz/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/SunnyData/solarViz/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/SunnyData/solarViz/node_modules/express/lib/router/layer.js:95:5)
at /SunnyData/solarViz/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/SunnyData/solarViz/node_modules/express/lib/router/index.js:335:12)
at next (/SunnyData/solarViz/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/SunnyData/solarViz/node_modules/express/lib/router/index.js:174:3)
at router (/SunnyData/solarViz/node_modules/express/lib/router/index.js:47:12)
毫无疑问,这是一个简单的问题,但我找不到以我能理解的方式涵盖这种情况的现有问题(我是 Node/Express 的新手,等等)
希望我已经提供了足够的信息。请询问我是否需要添加任何其他内容。
您应该在 ./services/solarData.js 的末尾添加以下行以导出函数;
module.exports.getMultiple = getMultiple;
它将允许 Node.js 导出函数并在其他文件中需要它
我一直在关注 tutorial 在 Node 中设置 REST API,使用 Express 访问现有 MariaDB 数据库的应用程序。我的版本只需要读取数据,并且我将 DB 与 Node 应用程序放在一起(同一主机)。
对于这个入门级示例,我的目标是仅使用静态 SQL 访问数据,因此我可以看到它由 JSON pritifier 在网页中呈现。
[接下来,我想在table(EJS?)中呈现数据。稍后,当我可以让它工作时,我将添加表单控件(React?)让用户指定 SQL 查询的开始和结束日期范围。最后,我的目标是将数据呈现为折线图 (D3js)。]
本教程成功运行了 Web 服务器(它 returns 'OK' 在基础 URL 上),但是当我转到 URL/solarData
时,它尝试使用异步函数来从数据库中获取多行,它响应 solarData.getMultiple is not a function TypeError: solarData.getMultiple is not a function
./app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 3800;
const solarDataRouter = require('./routes/solarData');
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
app.get('/', (req, res) => {
res.json({'message': 'ok'});
})
app.use('/solarData', solarDataRouter);
/* Error handler middleware */
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
console.error(err.message, err.stack);
res.status(statusCode).json({'message': err.message});
return;
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
./routes/solarData.js
const express = require('express');
const router = express.Router();
const solarData = require('../services/solarData');
/* GET solar data. */
router.get('/', async function(req, res, next) {
try {
res.json(await solarData.getMultiple(req.query.page));
} catch (err) {
console.error(`Error while getting solar data `, err.message);
next(err);
}
});
module.exports = router;
./config.js
const env = process.env;
const config = {
db: {
host: env.SUNNY_HOST,
user: env.SUNNY_USER,
password: env.SUNNY_PW,
database: env.SUNNY_DB,
},
listPerPage: env.LIST_PER_PAGE,
};
module.exports = config;
./services/solarData.js
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
}
}
./services/db.js
const mysql = require('mysql2/promise');
const config = require('../config');
async function query(sql, params) {
const connection = await mysql.createConnection(config.db);
const [results, ] = await connection.execute(sql, params);
return results;
}
module.exports = {
query
}
我遗漏了./helper.js
一切正常,直到我将网页指向 /solarData。那时我得到: 调试控制台 (vscode)
/Error while getting solar data solarData.getMultiple is not a function
routes/solarData.js:10
solarData.getMultiple is not a function TypeError: solarData.getMultiple is not a function
at /SunnyData/solarViz/routes/solarData.js:8:30
at Layer.handle [as handle_request] (/SunnyData/solarViz/node_modules/express/lib/router/layer.js:95:5)
at next (/SunnyData/solarViz/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/SunnyData/solarViz/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/SunnyData/solarViz/node_modules/express/lib/router/layer.js:95:5)
at /SunnyData/solarViz/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/SunnyData/solarViz/node_modules/express/lib/router/index.js:335:12)
at next (/SunnyData/solarViz/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/SunnyData/solarViz/node_modules/express/lib/router/index.js:174:3)
at router (/SunnyData/solarViz/node_modules/express/lib/router/index.js:47:12)
毫无疑问,这是一个简单的问题,但我找不到以我能理解的方式涵盖这种情况的现有问题(我是 Node/Express 的新手,等等)
希望我已经提供了足够的信息。请询问我是否需要添加任何其他内容。
您应该在 ./services/solarData.js 的末尾添加以下行以导出函数;
module.exports.getMultiple = getMultiple;
它将允许 Node.js 导出函数并在其他文件中需要它