【问题标题】:Mongoose Post with Mixed/Geospatial Schemas具有混合/地理空间模式的 Mongoose Post
【发布时间】:2013-07-13 23:45:26
【问题描述】:

您好,我正在使用 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")

【问题讨论】:

    标签: express mongoose


    【解决方案1】:

    您的函数签名表明没有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);
    });
    

    呃,刚刚看到这个帖子是尘土飞扬的。嗯……

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 2019-09-23
      • 2013-12-31
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多