4 mongoose mongodb geojson mongoose-schema
我有这个表单,我可以在其中获取用户的信息,该用户已正确保存了位置。
\n\n但是当我尝试更新它时,它给了我这个错误:
\n\n> Can\'t extract geo keys: { _id: ObjectId(\'5c4eb6c94f59c09ee7490ee0\'),\n> salt:\n> "d0ca72b02426c59da828fd65fff93e94ed7c4529f46db883c1f1cfa406be92ce",\n> hash:\n> "442a59e8ef32f4d309a1d2a71575c11a6d382e514d75f010e1228e8156ffa8ce71b7451c34b09319780744cc3e50e74e2e25d9ff75e8c7519087bddfa32b906801ed287ded16897c90ac15...",\n> email: "jorgeantonio82@gmail.com", name: "Jorge", location: {\n> coordinates: [ 13.42493130000003, 52.50074619999999 ], address:\n> "Naunynstra\xc3\x9fe 81, Berlin, Germany" }, created: new\n> Date(1548662473107), genres: null, props: [], __v: 0, musicLink:\n> "soundcloud" } unknown GeoJSON type: { coordinates: [\n> 13.42493130000003, 52.50074619999999 ], address: "Naunynstra\xc3\x9fe 81, Berlin, Germany" }\n
Run Code Online (Sandbox Code Playgroud)\n\n我的用户拥有我认为需要的所有东西:
\n\nconst userSchema = new Schema({\n email: {\n type: String,\n unique: true,\n lowercase: true,\n trim: true,\n validate: [validator.isEmail, \'Invalid Email Address\'],\n required: \'Please Supply an email address\'\n },\n name: {\n type: String,\n required: \'Please supply a name\',\n trim: true\n },\n resetPasswordToken: String,\n resetPasswordExpires: Date,\n props: [\n { type: mongoose.Schema.ObjectId, ref: \'User\' }\n ],\n // New stuff\n slug: String,\n genres: [String],\n musicLink: String,\n created: {\n type: Date,\n default: Date.now\n },\n location: {\n type: {\n type: String,\n default: \'Point\'\n },\n coordinates: [{\n type: Number,\n required: \'You must supply coordinates!\'\n }],\n address: {\n type: String,\n required: \'You must supply an address!\'\n }\n },\n photo: String,\n});\n\nuserSchema.index({ location: \'2dsphere\' })\n
Run Code Online (Sandbox Code Playgroud)\n\n当我保存它时,有一个控制器只推送某些内容来更新用户:
\n\nexports.updateAccount = async (req, res) => {\n const updates = {\n name: req.body.name,\n email: req.body.email,\n musicLink: req.body.musicLink,\n genres: req.body.tags,\n location: {\n address: req.body.location.address,\n coordinates: [\n req.body.location.coordinates[0],\n req.body.location.coordinates[1],\n ]\n }\n };\n\n console.log(\'coordinates: \', updates.location.coordinates)\n\n const user = await User.findOneAndUpdate(\n { _id: req.user._id },\n { $set: updates },\n { new: true, runValidators: true, context: \'query\' }\n );\n req.flash(\'success\', \'Updated the profile!\');\n res.redirect(\'back\');\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n知道可能是什么问题吗?
\n\n谢谢。
\n正如Mongoose 文档(以及GeoJSON 格式的基础标准)中所指定的,几何对象必须包含一个“几何类型”,其值为以下之一
七个区分大小写的字符串:“Point”、“MultiPoint”、“LineString”、“MultiLineString”、“Polygon”、“MultiPolygon”和“GeometryCollection”
错误信息
unknown GeoJSON type: { coordinates: [13.42493130000003, 52.50074619999999 ]
Run Code Online (Sandbox Code Playgroud)
指对象中缺少的类型属性。包括type
以避免错误:
{type: "Point", coordinates: [13.42493130000003, 52.50074619999999 ]}
Run Code Online (Sandbox Code Playgroud)