我正在尝试将 postgresql 数据库连接到我的后端和应用程序。获取数据库中的信息。我从 index.js 运行服务器并使用 shoesCtrl 作为检索信息的控制器。 每次我在 Postman 上测试我的端点时,我都会得到: “类型错误:dbInstance.read_shoes 不是 readShoes 的函数”
index.js
const express = require('express')
const app = express()
const shoesCtrl = require('./controllers/shoesCtrl')
const massive = require('massive')
const{SERVER_PORT, CONNECTION_STRING} = process.env
app.use(express.json())
massive({
connectionString: CONNECTION_STRING,
ssl: {
rejectUnauthorized: false
}
})
.then(dbInstance => {
app.set('db', dbInstance)
app.listen(SERVER_PORT, () => console.log(`Server is bumping on ${SERVER_PORT}`))
})
.catch(err => console.log(err))
app.get("/shoes", shoesCtrl.readShoes)
app.get("/shoes/:brand", shoesCtrl.readBrand)
shoesCtrl.js
module.exports = {
readShoes: (req, res) =>{
const dbInstance = req.app.get('db')
dbInstance.read_shoes()
.then(shoes => res.status(200).send(shoes))
.catch(err => {
res.sendStatus(500).send(console.log(err))
})
},
readBrand: (req, res) => {
const dbInstance = req.app.get('db')
const {brand} = req.params
dbInstance.read_product(brand)
.then(shoe => res.status(200).send(shoe))
.catch(err => {
res.sendStatus(500).send({errorMessage: "Oops! Something went wrong. Our engineers have been informed!"})
console.log(err)
})
}
我的 read_shoes.sql
select * from shoes;