jonasschmedtmann/complete-node-bootcamp

Issue in creating new tour - Can't extract geo keys

Closed this issue · 0 comments

Adding a new tour getting error - "Can't extract geo keys: { _id: ObjectId('60c04e193eda2e2b244cc074'), startLocation: { type: \"Point\", coordinates: [] }, ratingsAverage: 4.5, ratingsQuantity: 0, images: [], createdAt: new Date(1623215304931), startDates: [], secretTour: false, guides: [], imageCover: \"asdf.jpg\", summary: \"asdf\", price: 1, difficulty: \"easy\", maxGroupSize: 5, duration: 1, name: \"new test tour\", locations: [], slug: \"new-test-tour\", __v: 0 } Point must only contain numeric elements"
Postman

{
    "imageCover": "asdf.jpg",
    "summary": "asdf",
    "price": 1,
    "difficulty": "easy",
    "maxGroupSize": 5,
    "duration": 1,
    "name": "new test tour",
}

Model

startLocation: {
      // GeoJSON
      type: {
        type: String,
        default: "Point",
        enum: ["Point"],
      },
      coordinates: [Number],
      address: String,
      description: String,
    },
    locations: [
      {
        type: { type: String, default: "Point", enum: ["Point"] },
        coordinates: [Number],
        address: String,
        description: String,
        day: Number,
      },
    ],

tourSchema.index({ startLocation: "2dsphere" });

Controller

exports.getToursWithin = catchAsync(async (req, res, next) => {
 const { distance, latlng, unit } = req.params;
 const [lat, lng] = latlng.split(",");
 const radius = unit === "mi" ? distance / 3963.2 : distance / 6378.1;

 if (!lat || !lng) {
   next(
     new AppError(
       "Please provide latitude and longitude in the format lat,lng.",
       400
     )
   );
 }
 console.log(distance, lat, lng, unit);

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

 res.status(200).json({
   status: "success",
   results: tours.length,
   data: {
     data: tours,
   },
 });
});