私はSOの他のQ/Aを見ても解決できなかった問題に直面しています。ラインストリングとポリゴンを保存できません - 平均+マングース
私はMEANスタックアプリケーションへのインタフェースとしてGoogle Maps APIのを使用するWebアプリケーションを構築しています。またMongooseを使用してMongoDB Schemaを作成しています。
Unluckily, I'm not able to store LineStrings and Polygons. I'm only able to store Points and query them as I want (e.g. find closest points to another one).
私はPOST私は次のエラーを取得するラインストリングまたはポリゴンにしようとすると:
geoObjects validation failed
Cast to Array failed for value \"coordinates\" at path \"coordinates\"
Here's a Gist with the full Postman Log。
var mongoose = require('mongoose');
var GeoObjects = require('./model.js');
app.post('/geoObjects', function(req, res) {
// Creates a new Point based on the Mongoose schema and the post body
var newObj = new GeoObjects(req.body);
// New Points is saved in the db.
newObj.save(function(err) {
if (err){
res.send(err);
return;
}
// If no errors are found, it responds with a JSON of the new point
res.json(req.body);
});
});
そして、これらは私がしようとしているのLineStringとポリゴンの2つの例です:これは私のポストルート
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var geoObjects = new Schema({
name : {type: String},
type: {
type: String,
enum: [
"Point",
"LineString",
"Polygon"
],
default : 'Point'
},
coordinates: [Number],
});
geoObjects.index({coordinates: '2dsphere'});
module.exports = mongoose.model('geoObjects', geoObjects);
です:
は、ここに私のスキーマですPOST:
{
"name":"myPolygon",
"type": "Polygon",
"coordinates": [
[ [25.774, -80.190], [18.466, -66.118],
[32.321, -64.757], [25.774, -80.190]
]
]
}
{
"name":"myLineString",
"type": "LineString",
"coordinates": [
[17.811, 12.634], [12.039, 18.962],
[15.039, 18.962], [27.039, 18.962]
]
}
In a previous version, I had
coordinates: [Schema.Types.Mixed]
that allowed me to store all the 3 kind of geoObjects, but, unluckily, I was forced to switch to a different schema since withSchema.Types.Mixed
it was not possible to let my queries work on points.
- なぜ私はラインストリングとポリゴンを投稿することはできませんよ?
- どうすればこの問題を解決できますか?
ご迷惑をおかけして申し訳ありません。コメントを残してください。