JsS*_*ent 7 mongoose mongodb node.js express
我正在尝试查找特定距离内的旅行。这是我的代码
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
}
});
});
Run Code Online (Sandbox Code Playgroud)
但我在邮递员中收到此错误:
"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)"
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?谢谢
小智 26
我遇到了这个确切的错误。当您使用 async/await 时,您需要将“await”添加到对 Tours 模型的调用中。您还需要根据 @Anatoly 的观点包含要查询的字段名称:
const tours = await Tour.find({
yourfieldname: {
$geoWithin: { $centerSphere: [[lng, lat], radius] }
}
});
Run Code Online (Sandbox Code Playgroud)