Search code examples
mongodbmongoosegeojsonmongoose-schema

Can't extract geo keys, unknown GeoJSON type: { coordinates: [ 13.42493130000003, 52.50074619999999 ]


I have this form where I get the info of a user, the user has been saved with a location correctly.

But when i try to update it, it gives me this error back:

> Can't extract geo keys: { _id: ObjectId('5c4eb6c94f59c09ee7490ee0'),
> salt:
> "d0ca72b02426c59da828fd65fff93e94ed7c4529f46db883c1f1cfa406be92ce",
> hash:
> "442a59e8ef32f4d309a1d2a71575c11a6d382e514d75f010e1228e8156ffa8ce71b7451c34b09319780744cc3e50e74e2e25d9ff75e8c7519087bddfa32b906801ed287ded16897c90ac15...",
> email: "[email protected]", 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" }

My user has all the stuff i think it needs:

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

And when i save it there is a controller that pushes only certain stuff to update the user:

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

Any idea what might be the issue?

Thanks.


Solution

  • As specified in Mongoose docs (and in the underlying standard for the GeoJSON format), a geometry object must contain a "geometry type" with a value of one of

    seven case-sensitive strings: "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", and "GeometryCollection"

    The error message

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

    refers to the missing type property in the object. Include type to avoid the error:

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