gpt4 book ai didi

javascript - Express 中间件 : ERROR: TypeError: Converting circular structure to JSON

转载 作者:行者123 更新时间:2023-12-03 07:13:37 32 4
gpt4 key购买 nike

我使用以下函数作为中间件,只是为了增加添加到我的数组中的新对象的 id:

let lions = []
let id = 0

const updateId = function(req, res, next) {
if (!req.body.id) {
id++;
req.body.id = id + '';
}
next();
};

当我发布一只新狮子时,它会到达这条路线:
app.post('/lions', updateId, (req, res) => {
console.log('POST req', req.body)
const lion = req.body;
lions.push(lion)

res.json(req)
})

POST 工作并创建了新的狮子,但是我收到以下错误。关于如何解决它的任何想法?

[nodemon] starting node server.js NODE RUNNING on port: 3000 GET lions: [] ERROR: TypeError: Converting circular structure to JSON at JSON.stringify () at stringify (/Users/leongaban/projects/tutorials/pluralsight/api-design-node/node_modules/express/lib/response.js:1119:12)



服务器.js
// create a route middleware for POST /lions that will increment and
// add an id to the incoming new lion object on req.body

const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const port = 3000

app.use(express.static('client'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

let lions = []
let id = 0

const updateId = function(req, res, next) {
if (!req.body.id) {
id++;
req.body.id = id + '';
}
next();
};

app.param('id', (req, res, next, id) => {
let lion = lions.filter((lion => lion.id === id))

if (lion) {
req.lion = lion;
next();
}
else {
console.log('NO LION')
res.send()
}
})

app.get('/lions', (req, res, next) => {
console.log('GET lions:', lions)
res.json(lions)
})

app.get('/lions/:id', (req, res) => {
res.json(req || {})
})

app.post('/lions', updateId, (req, res) => {
console.log('POST req', req.body)
const lion = req.body;
lions.push(lion)

res.json(req)
})

app.put('/lions/:id', (req, res) => {
const paramId = req.params.id
const updated = req.body

if (updated.id) delete updated.id

const oldLion = lions.find((lion => lion.id === paramId))

if (!oldLion) res.send()

const newLion = Object.assign({ id: oldLion.id }, updated)
lions = lions.filter(lion => lion.id !== paramId)
lions.push(newLion)

res.json(newLion)
})

app.delete('/lions/:id', (req, res) => {
lions = lions.filter((lion => lion.id !== req.params.id))

res.json(lions)
})

app.use((err, req, res, next) => {
console.error('ERROR:', err)
})

app.listen(port, () => console.log(`NODE RUNNING on port: ${port}`))

最佳答案

Could be, maybe, because on this line: res.json(req) of the app.post() method, the req object contains an inner property referencing an outer one thus creating a circular reference. Check the structure of that object with console.log() or maybe you can avoid the problem if you return other thing on the response.

– Shidersz


需要在将其传递到 put 函数的 res.json 之前创建一个新变量
app.param('id', (req, res, next, id) => {
let lion = lions.filter((lion => lion.id === id))

if (lion) {
req.lion = lion;
next();
} else {
res.send();
}
})

app.get('/lions', (req, res, next) => {
console.log('GET lions:', lions)
res.json(lions)
})

app.get('/lions/:id', (req, res) => {
console.log('GET lion:', req.lion)
const lion = req.lion // <-- here
res.json(lion || {}) // <-- then here instead of passing req
})

关于javascript - Express 中间件 : ERROR: TypeError: Converting circular structure to JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503044/

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