I'm trying to follow the documentation for node mssql on creating pools.
I get this error...
TypeError: req.app.locals.pool.input is not a function
With this code...
server.js
const express = require("express");
const app = express();
const sql = require("mssql");
const { db } = require("./config.js");
const poolConnection = new sql.ConnectionPool(db);
const PORT = process.env.PORT || 9010;
//connect the pool and start the web server when done
poolConnection
.connect()
.then((pool) => {
app.locals.pool = pool; // <------- HERE
const server = require("http").createServer(app);
server.listen(PORT, () => {
console.log(`listening on port: ${PORT}`);
});
})
.catch((err) => {
console.error("Error creating connection pool", err);
});
route.js
router.get("/api/users/:userId", async (req, res, next) => {
try {
let userId = req.params.userId;
req.app.locals.pool.input("userId", sql.Int, userId); // <------- HERE
let query = `
select *
from bi_user u
where u.id = @userId
;
`;
let result = await req.app.locals.pool.query(query);
let final = result.recordset[0];
res.send(final);
} catch (err) {
next(err);
}
});
What am I doing wrong?
1条答案
按热度按时间vfh0ocws1#
Solved it like this...