如何修复TypeError.C的错误?将循环结构转换为JSON

我有一个简单的查询,我想从别名表中选择所有的人,之前一切都很好,但我做了npm更新。

TypeError: 将循环结构转换为JSON

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

但当我试图执行这个时,我得到了奇怪的错误。

(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.

有别名的 console.log.我也试过 await 和没有 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
}

await后的输出。

(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.

解决方案:

我建议你选择一个特定的字段来发送,比如说。

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

或者类似的东西来选择你需要的东西 而不是发送整个对象,因为它有一个循环结构。

给TA打赏
共{{data.count}}人
人已打赏
解决方案

在终端中的任意目录下运行 elasticsearch,并使用 "elasticsearch "命令。

2022-5-13 20:03:14

解决方案

ElasticSearch - 在两个时间段内比较不同的数值。

2022-5-13 20:03:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索