简体   繁体   English

Express Middleware:错误:TypeError:将循环结构转换为JSON

[英]Express Middleware: ERROR: TypeError: Converting circular structure to JSON

I'm using the following function as a middleware just to increment the id of a new object being added to my array: 我使用以下函数作为中间件只是为了增加添加到我的数组的新对象的id:

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. POST工作,并创建新狮子,但是我收到以下错误。 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) [nodemon]起始node server.js端口上的NODE RUNNING:3000 GET lions:[]错误:TypeError:在stringify上将JSON.stringify()处的循环结构转换为JSON(/ Users / leongaban / projects / tutorials / pluralsight / api-设计节点/ node_modules /表达/ LIB / response.js:1119:12)

server.js 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}`))

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. 可能是,因为在这一行:app.post()方法的res.json(req),req对象包含引用外部属性的内部属性,从而创建循环引用。 Check the structure of that object with console.log() or maybe you can avoid the problem if you return other thing on the response. 使用console.log()检查该对象的结构,或者如果在响应中返回其他内容,则可以避免此问题。

– Shidersz - Shidersz

Needed to create a new variable before passing it into the res.json of the put function 需要在将新变量传递给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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送非圆形 JSON 时的 Express 中间件“TypeError: Converting circular structure to JSON” - Express middleware "TypeError: Converting circular structure to JSON" when sending a non-circular JSON 'TypeError:使用 Express 将循环结构转换为 JSON' - 'TypeError: Converting circular structure to JSON' with Express TypeError:通过快递发送数组时将循环结构转换为 JSON - TypeError: Converting circular structure to JSON when sending array via express TypeError:将圆形结构转换为JSON - TypeError: Converting circular structure to JSON TypeError将循环结构转换为json - TypeError converting circular structure to json TypeError:将循环结构转换为JSON - 在json中查找错误 - TypeError: Converting circular structure to JSON - find error in json MongoDB和Express:类型错误:将循环结构转换为JSON - MongoDB and Express: Type Error: Converting Circular structure to JSON Angular Universal ERROR TypeError:将循环结构转换为 JSON - Angular Universal ERROR TypeError: Converting circular structure to JSON webpack构建错误TypeError:将圆形结构转换为JSON - webpack build error TypeError: Converting circular structure to JSON [Vue警告]:渲染错误:“TypeError:将循环结构转换为JSON - [Vue warn]: Error in render: "TypeError: Converting circular structure to JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM