【问题标题】:Can't extract geo keys, unknown GeoJSON type: { coordinates: [ 13.42493130000003, 52.50074619999999 ]无法提取地理键,未知 GeoJSON 类型:{ 坐标:[ 13.42493130000003, 52.50074619999999 ]
【发布时间】:2019-06-21 05:55:49
【问题描述】:

我有这个表格,我可以在其中获取用户的信息,用户已正确保存了位置。

但是当我尝试更新它时,它又给了我这个错误:

> Can't extract geo keys: { _id: ObjectId('5c4eb6c94f59c09ee7490ee0'),
> salt:
> "d0ca72b02426c59da828fd65fff93e94ed7c4529f46db883c1f1cfa406be92ce",
> hash:
> "442a59e8ef32f4d309a1d2a71575c11a6d382e514d75f010e1228e8156ffa8ce71b7451c34b09319780744cc3e50e74e2e25d9ff75e8c7519087bddfa32b906801ed287ded16897c90ac15...",
> email: "jorgeantonio82@gmail.com", name: "Jorge", location: {
> coordinates: [ 13.42493130000003, 52.50074619999999 ], address:
> "Naunynstraße 81, Berlin, Germany" }, created: new
> Date(1548662473107), genres: null, props: [], __v: 0, musicLink:
> "soundcloud" } unknown GeoJSON type: { coordinates: [
> 13.42493130000003, 52.50074619999999 ], address: "Naunynstraße 81, Berlin, Germany" }

我的用户拥有我认为需要的所有东西:

const userSchema = new Schema({
  email: {
    type: String,
    unique: true,
    lowercase: true,
    trim: true,
    validate: [validator.isEmail, 'Invalid Email Address'],
    required: 'Please Supply an email address'
  },
  name: {
    type: String,
    required: 'Please supply a name',
    trim: true
  },
  resetPasswordToken: String,
  resetPasswordExpires: Date,
  props: [
    { type: mongoose.Schema.ObjectId, ref: 'User' }
  ],
  // New stuff
  slug: String,
  genres: [String],
  musicLink: String,
  created: {
    type: Date,
    default: Date.now
  },
  location: {
    type: {
      type: String,
      default: 'Point'
    },
    coordinates: [{
      type: Number,
      required: 'You must supply coordinates!'
    }],
    address: {
      type: String,
      required: 'You must supply an address!'
    }
  },
  photo: String,
});

userSchema.index({ location: '2dsphere' })

当我保存它时,有一个控制器只推送某些内容来更新用户:

exports.updateAccount = async (req, res) => {
  const updates = {
    name: req.body.name,
    email: req.body.email,
    musicLink: req.body.musicLink,
    genres: req.body.tags,
    location: {
      address: req.body.location.address,
      coordinates: [
        req.body.location.coordinates[0],
        req.body.location.coordinates[1],
      ]
    }
  };

  console.log('coordinates: ', updates.location.coordinates)

  const user = await User.findOneAndUpdate(
    { _id: req.user._id },
    { $set: updates },
    { new: true, runValidators: true, context: 'query' }
  );
  req.flash('success', 'Updated the profile!');
  res.redirect('back');
};

知道可能是什么问题吗?

谢谢。

【问题讨论】:

  • 您需要在对象中有“类型”。例如。 {type: "point", coordinates: [...]}仅供参考mongoosejs.com/docs/geojson.html
  • 谢谢,这实际上是问题所在。如果您将其添加为答案,则将其标记为正确的答案。谢谢!

标签: mongodb mongoose geojson mongoose-schema


【解决方案1】:

Mongoose docs(以及GeoJSON 格式的underlying standard)中所指定,几何对象必须包含值为以下之一的“几何类型”

七个区分大小写的字符串:“Point”、“MultiPoint”、“LineString”、 “MultiLineString”、“Polygon”、“MultiPolygon”和“GeometryCollection”

错误信息

unknown GeoJSON type: { coordinates: [13.42493130000003, 52.50074619999999 ]

指对象中缺少的类型属性。包括type 以避免错误:

{type: "Point", coordinates: [13.42493130000003, 52.50074619999999 ]}

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 2016-03-05
    • 1970-01-01
    • 2020-07-20
    • 2020-07-31
    • 2019-04-06
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多