我正在使用 node.js 和 postgre 数据库创建一个 RESTful API。
我收到此错误:
<块引用>TypeError: 无法读取 null 的属性“1” 在 firstchar (/Users/samergesn/yachtClubServer/node_modules/body-parser/lib/types/json.js:176:37) 在解析 (/Users/samergesn/yachtClubServer/node_modules/body-parser/lib/types/json.js:79:19) 在 /Users/samergesn/yachtClubServer/node_modules/body-parser/lib/read.js:121:18 在 invokeCallback (/Users/samergesn/yachtClubServer/node_modules/raw-body/index.js:224:16) 完成 (/Users/samergesn/yachtClubServer/node_modules/raw-body/index.js:213:7) 在 IncomingMessage.onEnd (/Users/samergesn/yachtClubServer/node_modules/raw-body/index.js:273:7) 在 IncomingMessage.emit (events.js:314:20) 在 endReadableNT (_stream_readable.js:1223:12) 在 processTicksAndRejections (internal/process/task_queues.js:84:21)
下面是我的代码:
const express = require("express");
const app = express();
const Pool = require("pg").Pool
const pool = new Pool ({
user: 'postgres',
host:'localhost',
database: 'Yacht_Club',
password: 'Samerghosn4400',
port: 5432
});
app.use(express.json());
app.get("/members", async (req,res) => {
try{
const allMembers = await pool.query("SELECT * FROM members");
res.json(allMembers.rows);
}
catch(err) {
console.error(err.message)
}
});
app.listen(3000, () => {
console.log("server is listening on port 3000")
});