【问题标题】:Node.js, Mongoose, Express - Validate user input for longitude and latitudeNode.js、Mongoose、Express - 验证用户输入的经度和纬度
【发布时间】:2014-07-11 09:46:33
【问题描述】:

我正在寻找有关如何最好地验证通过 Express API 路由保存到 Mongodb 的 long/lat 值的建议。

我已经看到可以在模型中定义最小值和最大值的示例,但我无法让它工作。这是我当前的模型 - 请参阅“geo”:

var placeSchema = mongoose.Schema({
name: String,
geo: { type : [Number], index: '2d'},
tags: { type : [String], index: true },
create_date : {type: Date, "default": Date.now()}
});

我还可以在路由级别进行验证,或者在模型中使用 pre('save') 事件。

谁能告诉我应该在哪里做这个?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    这些代码怎么样

    placeSchema.path('geo').validate(function(geo){
       // do validation here
       // return geo.longitude > 0;
    }, 'Longitude should be larger than 0');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      相关资源
      最近更新 更多