如何解决Node.js / Postgres-服务器不接受JSON
我正在学习如何使用PERN堆栈,目前正在设置后端CRUD路由。我已经启动了Node.js服务器和Postgres数据库,但是由于某种原因,我的服务器无法编写将其传递给数据库的JSON。
我的Node.js代码如下:
app.post("/todos",async (req,res) => {
try {
const { description } = req.body;
const newTodo = await pool.query("INSERT INTO todo (description) VALUES($1) RETURNING *",[description]);
res.json(newTodo.rows[0]);
} catch (err) {
console.error(err.message);
}
});
我通过Insomnia将以下JSON传递给服务器:
{
"description":"Some new task"
}
失眠然后给我以下JSON:
{
"todo_id": 14,"description": null
}
进一步检查后,失眠给了我以下细节:
* Preparing request to http://localhost:5000/todos/
* Using libcurl/7.69.1 OpenSSL/1.1.1g zlib/1.2.11 brotli/1.0.7 libidn2/2.1.1 libssh2/1.9.0 nghttp2/1.40.0
* Current time is 2020-08-12T02:32:19.473Z
* Disable timeout
* Enable automatic URL encoding
* Enable SSL validation
* Enable cookie sending with jar of 6 cookies
* Connection 20 seems to be dead!
* Closing connection 20
* Trying ::1:5000...
* Connected to localhost (::1) port 5000 (#21)
> POST /todos/ HTTP/1.1
> Host: localhost:5000
> User-Agent: insomnia/2020.2.1
> Accept: */*
> Content-Length: 45
| {
| "description": "I need to clean my room"
| }
* upload completely sent off: 45 out of 45 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
< Content-Length: 33
< ETag: W/"21-35ITUmxJ2Do0aLZuKgJ4ee+3Kto"
< Date: Wed,12 Aug 2020 02:32:19 GMT
< Connection: keep-alive
* Received 33 B chunk
* Connection #21 to host localhost left intact
如果任何人对我的代码在我的路线上的错误之处有任何见识,那将是非常有帮助的。
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。