繁体   English   中英

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

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

我正在使用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);

当我尝试使用此架构保存新文档时:

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

它在给

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

但是,当我填写位置字段时,它运行良好。 我想知道为什么架构检查不需要的字段。

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);

并创建如下数据:

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

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

暂无
暂无

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

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