MongoDB $ geonear函数获取422错误

时间:2019-08-26 20:14:07

标签: node.js mongodb express geonear

我正在尝试使用mongodb $ geonear函数查找附近的对象。每当我在邮递员中启动get请求时,都会返回422错误代码。

我认为也许我的经度和纬度记录无效。我设置了有效的并且仍然收到相同的错误

$ geonear的代码

   router.get("/nearbypokemons", async (req, res) => {
  try {
  //finding nearby Pokemons using geo location
let nearByPokemons = await Pokemon.aggregate([
  {
    $geoNear: {
      near: {
        type: "Point",
        coordinates: [req.query.lng, req.query.lat]
      },
      spherical: true,
      maxDistance: 100000,
      distanceField: "dist.calculated"
    }
  }
]);

await res.send(nearByPokemons);
} catch (err) {
res.status(404).send({ error: err.message });
}});

MongoDB记录示例

      {
     "_id": "5d64292b7adf7804688f6806",
     "name": "dvdvvdsv",
    "rank": 222,
  "ability": "dvvsdvsd",
  "geometry": {
    "type": "Point",
    "_id": "5d64292b7adf7804688f6807",
    "coordinates": [
      22,
      33
    ]
  },
  "__v": 0
},
{
  "_id": "5d64395efaf93204880d0775",
  "name": "fefewfewfe",
  "rank": 111,
  "ability": "dvvdvdsv",
  "geometry": {
    "type": "Point",
    "_id": "5d64395efaf93204880d0776",
    "coordinates": [
      -73.99279,
      40.719296
    ]
  },
  "__v": 0
}

我希望它能输出至少一个附近的物体,但错误是相同的。

0 个答案:

没有答案