首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >geo元素必须是数组或对象: type:"Point“

geo元素必须是数组或对象: type:"Point“
EN

Stack Overflow用户
提问于 2018-09-04 06:10:56
回答 3查看 1.4K关注 0票数 2

当我尝试像这样发布请求的时候

{ "imgUrl":“服务器\图片\i 14182109167”,“文本”:“我在首尔”,“标签”:“汉城”,“旅游”,“几何”:{“类型”:“点”,“坐标”:80,-27}

错误原因

‘无法提取地理键:{ _id: ObjectId(’5b8e2044526366f86a6383‘),标签:“汉城”,“旅游”,日期:新日期(1536041028423),imgUrl:“服务器\图片\i 14182109167”,文本:“我在首尔”,几何学:{ type:"Point",坐标: 80,-27,_id: ObjectId(\’5b8e2044555266f86a6384) },__v: 0} geo元素必须是数组或对象:类型:“Point”}

甚至我也在post请求中添加了"type":"Point“,但是,为什么?

代码语言:javascript
复制
const geoSchema = new Schema({
  type: {
      type: String,
      default: 'Point',
      index: '2dsphere'
  },
  coordinates: {
      type: [Number]
  }
});

const memoSchema = new Schema({
  imgUrl: {
    type: String
  },
  text: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  },
  tag: {
    type: [String]
  },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'Memo'
  },
  geometry: geoSchema
})
EN

回答 3

Stack Overflow用户

发布于 2019-11-17 14:14:23

我经历了这个错误,尝试了几个模式声明,直到我通过实现这个设计来修复:

1.创建一个新模式,它将Point作为属性。

代码语言:javascript
复制
const mongoose = require('mongoose');
const {Point} = require('mongoose-geojson-schema');

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

2.对象的架构,包括上述属性:

代码语言:javascript
复制
const itemsSchema = new mongoose.Schema({
      description: {
        type: String,
        required: true
      },
      location: {
        type: pointSchema,
        required: true
      }
)
const Item = mongoose.model('Item', ItemsSchema);
module.exports = Item;
    enter code here

3.最后,我的控制器成功地实例化了对象,如下所示:

代码语言:javascript
复制
  var geolocation =
  {
    type: 'Point',
    coordinates: [
      lng,
      lat
    ]
  };


 const item = new Item({
     description: req.body.description,
     location: geolocation
 })

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2018-09-04 12:00:38

我改变了几何学,让它成功了!但是我不知道为什么..。

票数 0
EN

Stack Overflow用户

发布于 2022-08-19 08:30:39

即使在做了这里提到的所有事情之后,我也面临着同样的问题。在这个问题上花了太多时间之后,我知道如果我们保存一个默认值,它将正确工作。

只需按照上面的答案添加

代码语言:javascript
复制
location: {
  type: pointSchema,
  default: {
    type: 'Point',
    coordinates: [0, 0],
  },
  required: false,
},

如果任何文档都有位置,则删除以前的文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52159814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档