混合/地理空间模式的Mongoose Post



你好,我正在使用Mongoose和Express提交地图的地理空间数据(GEOJSON)。我有一个表格,它可以获得一个点的经度和纬度,然后用户可以提交来保存这个点。

如果我在发布路线的"坐标"部分对值进行硬编码,我的表单就会工作,但如果我尝试执行req.body.longitude和req.body.latitude,它不会发布到数组,并会给我一个"req not defined"错误。

我在这里学习了mongoose geojson的基本知识:https://gist.github.com/aheckmann/5241574

如何在混合模式中从req.body值保存此表单?谢谢

我的模式

var schema = new Schema({
  type: {type: String},
  properties: {
    popupContent: {type: String}
  },
  geometry: {
      type: { type: String }
    , coordinates: {}
  }
});
schema.index({ geometry: '2dsphere' });
var A = mongoose.model('A', schema);

我的邮寄路线

    app.post('/api/map', function( request, response ) {
      console.log("Posting a Marker");
        var sticker = new A({
        type: 'Feature',
        properties: {
          popupContent: 'compa'
        },
    geometry: {
      type: 'Point',
      coordinates: [req.body.longitude, req.body.latitude]
    }
  });
sticker.save();
  return response.send( sticker );
  res.redirect('/map')
   });

我的客户端表单

 form(method='post', action='/api/map') 
  input#popup(type="text", value="click a button", name="popup")
  input#lng(type="text", value="click a button", name="longtude")
  input#lat(type="text", value="click a button", name="latitude")
  input(type="submit")

您的函数签名声明不存在req参数。

 app.post('/api/map', function( request, response )

您应该在签名或正文中重命名参数。

app.post('/api/map', function(request, response) {
  console.log("Posting a Marker");
  var sticker = new A({
    type: 'Feature',
    properties: {
      popupContent: 'compa'
    },
    geometry: {
      type: 'Point',
      coordinates: [request.body.longitude, request.body.latitude]
    }
  });
  sticker.save();
  return response.send(sticker);
});

刚刚看到这条线满是灰尘。嗯…

最新更新