无法提取地理位置密钥,未知的GeoJSON类型:{坐标:[13.42493130000003,52.50074619999999]

时间:2019-01-28 08:59:43

标签: mongodb mongoose geojson mongoose-schema

我有此表格可获取用户信息,该用户已正确保存了位置。

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

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

任何想法可能是什么问题?

谢谢。

1 个答案:

答案 0 :(得分:1)

根据Mongoose docs(对于GeoJSON格式,在underlying standard中指定),几何对象必须包含值为

之一的“几何类型”
  

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

错误消息

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

引用对象中缺少的type属性。包括type以避免错误:

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