Hello, I’m getting an error when using mongoose to create a “Point” here is my mongoose schema
const providerSchema = new mongoose.Schema(
{
admin: { type: Schema.Types.ObjectId, ref: "User" },
serviceList: {
type: [String], // Define serviceList as an array of strings
default: [],
},
serviceGEOS: [
{
type: {
type: String, // Must be 'Point' for GeoJSON
enum: ["Point"], // Enforce 'Point' as the only allowed value
required: true,
},
coordinates: {
type: [Number], // Array of numbers: [longitude, latitude] Note that longitude comes first in a GeoJSON coordinate array, not latitude.
required: true,
},
// city: String,
// state: String,
// address: String,
// postalCode: String,
// county: String,
// placeID: String,
},
],
bio: String,
serviceAreas: [Object],
milageRate: Number,
businessName: String,
user: { type: Schema.Types.ObjectId, ref: "User", required: true },
businessType: String,
businessPhone: String,
counties: [Object],
businessAddress: {
street: String,
city: String,
state: String,
zip: String,
description: String,
bld: String,
placeID: String,
},
buildingNumber: String,
users: [{ type: Schema.Types.ObjectId, ref: "User" }],
businessEmail: String,
rating: Number,
reviews: [{ type: Schema.Types.ObjectId, ref: "Review" }],
subscription_id: String,
plan: String,
onboarded: { type: Boolean, default: false },
},
{ timestamps: true }
);```
Here is the code in my express router that is meant to update the provider model and add a list to serviceGEOs (the GEOJSON object) `const serviceGeoList = geoList.map((geo) => {
// Ensure lat/lng are valid numbers
const lng = parseFloat(geo.lng);
const lat = parseFloatgeo.lat);
if (isNaN(lng) || isNaN(lat)) {
throw new Error(`Invalid coordinates for geo: ${JSON.stringify(geo)}`);
}
return {
type: "Point",
coordinates: [lng, lat], // Ensure proper GeoJSON format with numeric values
// city: geo.city,
// state: geo.state,
// postalCode: geo.postalCode,
// placeID: geo.placeID,
// description: geo.description,
};
});
await Provider.updateOne({
admin: userData._id,
$set: {
serviceGEOS: [serviceGeoList],
},
});
await userProvider.save();
`
Keep in mind the serviceGeoList that is being iterated over is an array of objects similar to this
{
city: "Montclair"
county: "San Bernardino County"
description: "Montclair, CA 91762, USA"
lat: 34.0420153
lng: -117.6610854
placeID: "ChIJi3unimExw4AR7Y0RLUui4d8"
postalCode: "91762"
state: "California"
}
Can someone please assist me with handling this error and getting the GEOJSON points into my database