首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用express、mongodb时出现错误"Converting circular to JSON“

使用express、mongodb时出现错误"Converting circular to JSON“
EN

Stack Overflow用户
提问于 2020-09-20 23:39:06
回答 2查看 1.3K关注 0票数 2

我正在尝试查找特定distance.here内的旅游是我的代码

代码语言:javascript
运行
复制
exports.getTourWithin = catchAsync(async (req, res, next) => {
  const { distance, latlng, unit } = req.params;
  const [lat, lng] = latlng.split(',');
  if (!lat || !lng) {
    next(
      new AppError(
        `please provide latitude amd longitude in the form of lat,lng`,
        400
      )
    );
  }
  const radius = unit === 'mi' ? distance / 3963.2 : distance / 6378.1;

  const tours = Tour.find({
    $geoWithin: { $centerSphere: [[lng, lat], radius] }
  });
  res.status(200).json({
    status: 'success',
    data: {
      data: tours
    }
  });
});

但是我在邮递员中发现了这个错误:

代码语言:javascript
运行
复制
"message": "Converting circular structure to JSON\n    --> starting at object with constructor 'NativeTopology'\n    |     property 's' -> object with constructor 'Object'\n    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'\n    --- property 'topology' closes the circle",
    "stack": "TypeError: Converting circular structure to JSON\n    --> starting at object with constructor 'NativeTopology'\n    |     property 's' -> object with constructor 'Object'\n    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'\n    --- property 'topology' closes the circle\n    at JSON.stringify (<anonymous>)\n    at stringify (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\response.js:1119:12)\n    at ServerResponse.json (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\response.js:260:14)\n    at D:\\Node-Jonas\\Node-Rest-Api\\controllers\\tourControllers.js:190:19\n    at D:\\Node-Jonas\\Node-Rest-Api\\utilis\\catchAsync.js:3:5\n    at Layer.handle [as handle_request] (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at next (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\route.js:137:13)\n    at Route.dispatch (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\route.js:112:3)\n    at Layer.handle [as handle_request] (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\layer.js:95:5)\n    at D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:281:22\n    at param (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:354:14)\n    at param (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:365:14)\n    at param (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:365:14)\n    at param (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:365:14)\n    at Function.process_params (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:410:3)\n    at next (D:\\Node-Jonas\\Node-Rest-Api\\node_modules\\express\\lib\\router\\index.js:275:10)"
}

我该如何解决这个问题?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-26 04:04:37

我遇到了这个确切的错误。在使用async/await时,您需要将"await“添加到对your模型的调用中。根据@Anatoly的观点,您还需要包含要查询的字段名称:

代码语言:javascript
运行
复制
const tours = await Tour.find({
    yourfieldname: {
        $geoWithin: { $centerSphere: [[lng, lat], radius] }
    }
});
票数 4
EN

Stack Overflow用户

发布于 2020-09-20 23:49:55

tours中,你得到的是一个游标,而不是数据本身。您应该调用toArray来检索数据。

代码语言:javascript
运行
复制
const tours = Tour.find({
    $geoWithin: { $centerSphere: [[lng, lat], radius] }
  });
res.status(200).json({
    status: 'success',
    data: {
      data: tours.toArray()
    }
  });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63980737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档