【发布时间】:2021-07-17 14:35:17
【问题描述】:
当我尝试从数据库中获取数据时遇到这个问题
TypeError: 无法读取未定义的属性“findAll”
我使用 react 和 node.js + postgresql 制作了网络。 Postgres 在我们的服务器中,所以我没有使用本地主机。我尝试了其他一些帖子,但一切正常。
服务器.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
var corsOptions = {
origin: "http://localhost:3000",
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to bezkoder application." });
});
// set port, listen for requests
require("./routes/noticias.routes")(app);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
controller.js
const db = require("../config/db.config");
const noticias = db.noticias;
// Retrieve all noticias from the database.
const getNews = (req, res) => {
noticias
.findAll({})
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message: err.message || "Some error occurred.",
});
});
};
module.exports = {
getNews,
};
路由器
module.exports = (app) => {
const noticias = require("../controllers/noticias.controller.js");
var router = require("express").Router();
router.get("/", noticias.getNews);
app.use("/noticias", router);
};
感谢您的帮助^^
【问题讨论】:
-
在
const noticias = datab.noticias;之前添加console.log(datab);。你看到了什么? -
datab.noticias未定义
标签: javascript node.js postgresql