Skip to content

fix geo #1

@FaridSafi

Description

@FaridSafi
// Add in Model mongoDB
​
location: {
    type: {
      type: String,
      enum: [“Point”],
      default: “Point”
    },
    coordinates: {
      type: [Number],
      default: [0, 0]
    }
  },
  
  // Find with mongoose
   const { longitude, latitude, radius } = req.fields;
  let KM = radius / 1000;
  if (!longitude || !latitude || !radius) {
    res.status(400).json({ error: "Missing Geoloc parameters" });
  } else {
    const allProducers = await Producers.find({
      location: {
        $geoWithin: { $centerSphere: [[longitude, latitude], KM / 6371] }
      }
    });
    res.json(allProducers);
  }
  
  //Update field in New model 
 location: { type: "Point", coordinates: [longitude, latitude] },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions