Javascript TypeError:无法读取未定义的-Node.js+Postgresql的属性“findAll”
当我试图从数据库中获取数据时,我遇到了这个问题 TypeError:无法读取未定义的属性“findAll” 我使用react和node.js+postgresql创建了这个网站。Postgres在我们的服务器中,所以我没有使用本地主机。我尝试了其他的帖子,但一切都有效 Server.jsJavascript TypeError:无法读取未定义的-Node.js+Postgresql的属性“findAll”,javascript,node.js,postgresql,Javascript,Node.js,Postgresql,当我试图从数据库中获取数据时,我遇到了这个问题 TypeError:无法读取未定义的属性“findAll” 我使用react和node.js+postgresql创建了这个网站。Postgres在我们的服务器中,所以我没有使用本地主机。我尝试了其他的帖子,但一切都有效 Server.js const express = require("express"); const bodyParser = require("body-parser"); const
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);
};
谢谢你的帮助^ ^我对knex不太了解。据我从文档中了解,knex中有一个选择功能,而不是findAll 文件: 请尝试下面的方法,看看会发生什么
// below line is not required;
// const noticias = datab.noticias;
// Retrieve all noticias from the database.
const getNews = (req, res) => {
datab
.select("column_name_1", "column_name_2", "...")
.from("noticias")
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message: err.message || "Some error occurred.",
});
});
};
PS:findAll函数在sequelize而不是knex中可用。使用数据库。选择“*”。从“用户”到findAll添加console.logdatab;在const noticias=datab.noticias;之前;。你看到了什么?datab.noticias未定义