javascript - TypeError:无法读取未定义的属性“findAll”-Node.js + Postgresql
问题描述
当我尝试从数据库中获取数据时,我遇到了这个问题
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}.`);
});
控制器.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 中select
没有一个函数。findAll
文档:https ://knexjs.org/#Builder-select
请尝试以下方法,看看会发生什么
// 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
not中可用knex
。
推荐阅读
- javascript - 使用子-父依赖项对数组进行排序
- azure-devops - 通过 echo somtimes 设置变量会在末尾添加一个随机 '
- git - 来自分支 X 的分支 Y - 如何仅刷新原始分支 X?
- amazon-web-services - 跨实例的 aws 内存消耗问题
- docker - 如何从 docker-compose config 生成的文件中删除绝对路径?
- c# - 将字节数组转回 img
- html - 如何通过可下载的链接将文本保存为 .txt 文件?
- regex - 正则表达式删除部分字符串并替换另一部分
- accessibility - Office ui fabric Dropdown 读取组件焦点上的选定项
- javascript - 这在 Vue 中是未定义的,使用 debounce 方法