简体   繁体   中英

Mongoose giving error for NOT required Geo spatial field

I am using Mongoose Schema and simplified version looks like:

const newSchema = new mongoose.Schema({
  name:{
    type: String,
    required: true
  },
  location:{
    type: {
      type: String,
      default: 'Point',
      enum: ['Point']
    },
    coordinates: [Number],
  }
});

const newModel = mongoose.model('NewModel', newSchema);

When I try to save a new document using this schema:

newModel.create({
  "name": "Default name"
});

It is giving

error: 
"Can't extract geo keys: { _id: ObjectId('5e8ed5ddf4781c24d0836b6e'), location: { type: \"Point\" },
 name:\"Default name\"} Point must be an array or object"

However, when I fill the location field, it works well. I am wondering why Schema checking for the NOT required field.

var GeoJSON = require('mongoose-geojson-schema');
var mongoose = require('mongoose');
const newSchema = new mongoose.Schema({
  name:{
    type: String,
    required: true
  },
  location:mongoose.Schema.Types.GeoJSON
});

const newModel = mongoose.model('NewModel', newSchema);

and create data like:

newModel.create({
  name: "Default name",
   location: {
          type: "Point",
          coordinates: [longitude, latitude]
        }
});

actually the problem occur due to you specified the default type is Point (that pushed by mongoose always if null or not specified) but you did't pass coordinates corresponding, and the point should be with coordinate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM