简体   繁体   English

如何修复 TypeError:将循环结构转换为 JSON

[英]How to fix TypeError: Converting circular structure to JSON

I got simple query where I want to select all from aliases table.我从别名表中得到了我想要 select 的简单查询。 Before all works fine but I did npm update.在一切正常之前,但我做了 npm 更新。

TypeError: Converting circular structure to JSON TypeError:将循环结构转换为 JSON

 public async aliasesList(req: Request, res: Response): Promise<void> { 
            const aliases = await pool.query('SELECT * FROM aliases');
                res.json(aliases);
        }

But when I trying to execute this I got strange error:但是当我尝试执行此操作时,出现了奇怪的错误:

(node:20996) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Query'
    |     property '_timer' -> object with constructor 'Timer'
    --- property '_object' closes the circle
    at JSON.stringify (<anonymous>)
    at C:\Users\rad\Desktop\Projekty\inv\backend\app\controllers\aliasesController.ts:27:14
    at Generator.next (<anonymous>)
    at C:\Users\rad\Desktop\Projekty\inv\backend\app\controllers\aliasesController.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\rad\Desktop\Projekty\inv\backend\app\controllers\aliasesController.ts:4:12)
    at aliasesList (C:\Users\rad\Desktop\Projekty\inv\backend\app\controllers\aliasesController.ts:35:16)
    at Layer.handle [as handle_request] (C:\Users\rad\Desktop\Projekty\inv\backend\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\rad\Desktop\Projekty\inv\backend\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\rad\Desktop\Projekty\inv\backend\node_modules\express\lib\router\route.js:112:3)
(node:20996) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20996) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with 
a non-zero exit code.

There is console.log of aliases.有别名的 console.log。 I tried also await and without await我也试过等待和没有等待

Query {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  _callback: undefined,
  _callSite: Error:
      at Pool.query (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\mysql\lib\Pool.js:199:23)
      at C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:29:36
      at Generator.next (<anonymous>)
      at C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:8:71
      at new Promise (<anonymous>)
      at __awaiter (C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:4:12)
      at aliasesList (C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:39:16)
      at Layer.handle [as handle_request] (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\express\lib\router\layer.js:95:5)
      at next (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\express\lib\router\route.js:137:13)
      at Route.dispatch (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\express\lib\router\route.js:112:3),
  _ended: false,
  _timeout: undefined,
  _timer: Timer { _object: [Circular], _timeout: null },
  sql: 'SELECT * FROM aliases',
  values: undefined,
  typeCast: true,
  nestTables: false,
  _resultSet: null,
  _results: [],
  _fields: [],
  _index: 0,
  _loadError: null
}

Output after await: Output 等待后:

(node:32140) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Query'
    |     property '_timer' -> object with constructor 'Timer'
    --- property '_object' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (C:\Users\rad\Desktop\Projects\inv\backend\node_modules\express\lib\response.js:260:14)
    at C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:30:25
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\rad\Desktop\Projects\inv\backend\app\controllers\aliasesController.ts:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:32140) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:32140) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with 
a non-zero exit code.

OK cool, you can see in the output that you have a circular JSON object right?好的酷,您可以在 output 中看到您有一个圆形 JSON object 对吗? what I suggest you choose a specific field to send for example:我建议您选择要发送的特定字段,例如:

public async aliasesList(req: Request, res: Response): Promise<void> { 
  const aliases = await pool.query('SELECT * FROM aliases');
  res.json(aliases._results);
}

Or something of that sort to just select what you need instead of sending the entire object which has a circular structure.或者类似的东西只是 select 你需要什么,而不是发送整个 object ,它具有一个圆形结构。

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

相关问题 如何修复 TypeError:在 JSON.stringify 处将循环结构转换为 JSON (<anonymous> ) 在本机中 - How to fix TypeError: Converting circular structure to JSON at JSON.stringify (<anonymous>) in react-native TypeError将循环结构转换为json - TypeError converting circular structure to json TypeError:将圆形结构转换为JSON - TypeError: Converting circular structure to JSON 如何解决&#39;未捕获的TypeError:将圆形结构转换为JSON&#39; - How to solve ' Uncaught TypeError: Converting circular structure to JSON ' 如何解决 NodeJS 中的“TypeError:将循环结构转换为 JSON” - How do I solve “TypeError: Converting circular structure to JSON” in NodeJS 如何在JSON.stringify中找到循环结构:Uncaught TypeError:将循环结构转换为JSON? - How to find the circular structure in JSON.stringify: Uncaught TypeError: Converting circular structure to JSON? Mocha:TypeError:将循环结构转换为 JSON - Mocha: TypeError: Converting circular structure to JSON 椭圆:TypeError:将圆形结构转换为 JSON - Elliptic : TypeError : Converting circular structure to JSON 类型错误:在 mongoDB/mongoose 中将循环结构转换为 JSON - TypeError: Converting circular structure to JSON in mongoDB/mongoose 'TypeError:使用 Express 将循环结构转换为 JSON' - 'TypeError: Converting circular structure to JSON' with Express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM