简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z 为不需要的地理空间字段提供错误

[英]Mongoose giving error for NOT required Geo spatial field

I am using Mongoose Schema and simplified version looks like:我正在使用Mongoose Schema ,简化版本如下所示:

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实际上问题是由于您指定的默认类型是 Point (如果 null 或未指定,则始终由 mongoose 推送)但是您没有传递对应的坐标,并且该点应该是坐标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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