mongoose 在使用express,mongodb时出现错误“将循环结构转换为JSON”

fnx2tebb  于 2022-11-13  发布在  Go
关注(0)|答案(5)|浏览(231)

我试图在一个特定的www.example.com内查找旅游distance.here是我的代码

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

但是我在postman中得到这个错误:

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

我怎么能解决这个问题?谢谢

c86crjj0

c86crjj01#

我遇到了这个错误。当你使用async/await时,你需要在调用图尔斯模型时添加“await”。你还需要包括你在@Anatoly的点上查询的字段名:

const tours = await Tour.find({
    yourfieldname: {
        $geoWithin: { $centerSphere: [[lng, lat], radius] }
    }
});
2skhul33

2skhul332#

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

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

hfwmuf9z3#

在我的例子中,我忘了在mongo的find()操作前面放await,在那个语句中添加await后,它对我起作用了,所以它返回promise而不是期望的结果
为了更好地理解,请阅读一次https://www.geeksforgeeks.org/mongodb-db-collection-find-method/

wqnecbli

wqnecbli4#

因为你使用的是异步,所以你必须在Find()函数中添加await。在我的例子中,同样的错误发生了,并通过等待解决。

pinkon5k

pinkon5k5#

您可能会错过“await”关键字。

相关问题