Gufran Karim
Gufran Karim

Reputation: 11

How to fix location.type: Path `location.type` is required

I created model, and i want to save it to my mongoDB Compass.

const BootcampSchema = new mongoose.Schema({

location:{
    // GeoJSON Point
    type: {
        type: String,
        enum: ['Point'],
        required: true
    },
    coordinates: {
        type: [Number],
        required: true,
        index: '2dsphere',
        sparse: true,
    },
    formattedAddress: String,
    street: String,
    city: String,
    state: String,
    zipcode: String,
    country: String
});

press

Upvotes: 0

Views: 330

Answers (2)

Sanjay RD
Sanjay RD

Reputation: 91

All you have to remove or change required to false code

location:{
    // GeoJSON Point
    type: {
        type: String,
        enum: ['Point'],
        required: false
    },
    coordinates: {
        type: [Number],
        required: false,
        index: '2dsphere',
        sparse: true,
    },
    formattedAddress: String,
    street: String,
    city: String,
    state: String,
    zipcode: String,
    country: String
});

Upvotes: 1

Kowshik Sundararajan
Kowshik Sundararajan

Reputation: 13

When saving a Bootcamp document, you have to ensure that the location field has a type property. It should look something like this:

{
  ...,
  location: {
    type: ...,
    coordinates: ...
  }
  ...
}

Upvotes: 0

Related Questions