admin管理员组文章数量:1292430
I'm using the following function as a middleware just to increment the id of a new object being added to my array:
let lions = []
let id = 0
const updateId = function(req, res, next) {
if (!req.body.id) {
id++;
req.body.id = id + '';
}
next();
};
When I post a new lion it will then hit this route:
app.post('/lions', updateId, (req, res) => {
console.log('POST req', req.body)
const lion = req.body;
lions.push(lion)
res.json(req)
})
The POST works and the new lion is created, however I get the following error. Any ideas on how to fix it?
[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)
server.js
// create a route middleware for POST /lions that will increment and
// add an id to the ining 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}`))
I'm using the following function as a middleware just to increment the id of a new object being added to my array:
let lions = []
let id = 0
const updateId = function(req, res, next) {
if (!req.body.id) {
id++;
req.body.id = id + '';
}
next();
};
When I post a new lion it will then hit this route:
app.post('/lions', updateId, (req, res) => {
console.log('POST req', req.body)
const lion = req.body;
lions.push(lion)
res.json(req)
})
The POST works and the new lion is created, however I get the following error. Any ideas on how to fix it?
[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)
server.js
// create a route middleware for POST /lions that will increment and
// add an id to the ining 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}`))
Share
Improve this question
edited Apr 3, 2019 at 19:55
Leon Gaban
asked Apr 3, 2019 at 19:37
Leon GabanLeon Gaban
39k122 gold badges348 silver badges550 bronze badges
4
-
3
Could be, maybe, because on this line:
res.json(req)
of theapp.post()
method, thereq
object contains an inner property referencing an outer one thus creating a circular reference. Check the structure of that object withconsole.log()
or maybe you can avoid the problem if you return other thing on the response. – Shidersz Commented Apr 3, 2019 at 19:50 - Maybe this could be of help too: stackoverflow./questions/11616630/… – Shidersz Commented Apr 3, 2019 at 19:53
- @Shidersz you were right, posting what I did to fix the problem, but do you want to post your answer? I'll check it. – Leon Gaban Commented Apr 3, 2019 at 20:01
- Sorry, but I don't see a clear way to make an answer for this issue under the context you provided. However I'm glad that the hypothesis of the possible issue helps you. – Shidersz Commented Apr 3, 2019 at 20:12
1 Answer
Reset to default 5Could 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
Needed to create a new variable before passing it into the res.json of the put function
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
})
本文标签: javascriptExpress Middleware ERROR TypeError Converting circular structure to JSONStack Overflow
版权声明:本文标题:javascript - Express Middleware: ERROR: TypeError: Converting circular structure to JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741555469a2385128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论