[英]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.