gpt4 book ai didi

node.js - 在 Nodejs-Express API 中 Sequelize body-parser 错误

转载 作者:行者123 更新时间:2023-12-03 22:32:17 27 4
gpt4 key购买 nike

我有 Nodejs-express-postgres API,它使用 sequalize ORM,它只需添加捐助者的详细信息并获取。添加工作正常,但在获取时出现以下错误

SyntaxError: Unexpected token   in JSO:10)N at position 0                       s\api-server-sequalize\node_modules\body-parser\l
at JSON.parse (<anonymous>)
at createStrictSyntaxError (E:\Noderver-sequalize\node_modules\body-parser\lib\readeProjects\red_lines\api-server-sequalize\node_modules\body-parser\lib\types\\red_lines\api-server-sequalize\node_modules\raw-json.js:158:10)
at parse (E:\NodeProjects\red_line\api-server-sequalize\node_modules\raw-body\indexs\api-server-sequalize\node_modules\body-parser\lib\types\json.js:83:15) rojects\red_lines\api-server-sequalize\node_modul
at E:\NodeProjects\red_lines\api-smingMessage.emit (node:events:388:22)erver-sequalize\node_modules\body-parsreams/readable:1305:12)er\lib\read.js:121:18 :internal/process/task_queues:80:21)
at invokeCallback (E:\NodeProjects\red_lines\api-server-sequalize\node_modules\raw-body\index.js:224:16)
at done (E:\NodeProjects\red_lines\api-server-sequalize\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (E:\NodeProjects\red_lines\api-server-sequalize\node_modules\raw-body\index.js:273:7) at IncomingMessage.emit (node:events:388:22)
我的路线代码如下所示
const express = require("express");
const router = new express.Router();
const Donor = require("../models/donorModel");

router.get('/', (req, res) => {
res.status(201).send("Working");
});

router.get('/donor', async(req, res) => {
Donor.findAll().then((model)=>{

res.status(201).send(model)
}).catch((e)=>{
console.log('got some error')
res.status(401).send(e)
})
})
router.post("/donor", (req, res) => {
const { dname, dlocation,dphone , demail, ddob ,dbgroup } = req.body;

Donor.create({
dname: dname,
ddob: ddob,
demail: demail,
dlocation: dlocation,
dphone: dphone,
dbgroup: dbgroup,
}).then((model) => {
console.log("Saved");
res.status(201).send(model);
})
.catch((error) => {
res.status(201).send(error);
});
});


module.exports = router;
和 index.js 文件如下
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const db = require("./app/config/database");
require("dotenv").config();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(require("./app/routes/routes"));
//Test DB
db.authenticate()
.then(() => console.log("Database connected"))
.catch(e => console.log("Error:" + e))
// routes

app.listen(process.env.NODE_PORT || 3000, () =>
console.log(`server running on PORT ${process.env.NODE_PORT} !`)
);
希望有人能解决

最佳答案

在您的 route ,将数据转换为 json,然后再将其作为响应发送。
例如,在“/donor” route ,您可以这样做:res.status(201).json(model);

关于node.js - 在 Nodejs-Express API 中 Sequelize body-parser 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65722644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com