Express中间件:错误:TypeError:将圆形结构转换为JSON [英] Express Middleware: ERROR: TypeError: Converting circular structure to JSON

查看:72
本文介绍了Express中间件:错误:TypeError:将圆形结构转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]从node server.js开始 节点在端口上运行:3000 取得狮子:[] 错误:TypeError:将圆形结构转换为JSON 在JSON.stringify() 在stringify(/Users/leongaban/projects/tutorials/pluralsight/api-design-node/node_modules/express/lib/response.js:1119:12)

[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 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}`))

推荐答案

可能是因为在以下这一行上:app.post()方法的res.json(req),req对象包含一个内部属性,该内部属性引用一个外部属性,从而创建了一个循环引用.使用console.log()检查该对象的结构,或者如果在响应中返回其他内容,则可以避免该问题.

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

– Shidersz

需要先创建一个新变量,然后再将其传递到put函数的res.json中

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
})

这篇关于Express中间件:错误:TypeError:将圆形结构转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆