【问题标题】:Mongoose Validation using Feathersjs and Vue使用 Feathersjs 和 Vue 进行 Mongoose 验证
【发布时间】:2016-12-24 02:29:43
【问题描述】:

如何在 Mongoose with Feathers/Vue 堆栈中对查询使用验证?这就是我正在经历的。传递给 feathers-mongoose 服务的任何查询条件都必须在 Vue 模型中传递,以便它运行查询,过滤掉没有名称属性或根本没有字段的返回项。这是它不起作用时的样子。注意“真实”结果。

  var vm = new Vue({
  el: '#app',
  data () {
      return {
          places:[]
      }
  },
  ready () {
        // Find all places
        placeService.find({
            query: {
               ** name: { $exists: true } **
            }
        }).then(page => {
          this.places = page.data
        })
    }
})

如果您在服务的“选项”中添加此项,则查询最终会显示在 index.html 的 {{ i.name }} 中显示为“true”的项目。这是服务设置:

module.exports = function() {
  const app = this;

  const options = {
    Model: place,
    paginate: {
      default: 15,
      max: 30
    },
    // query: {
    //     name: { $exists: true },
    //     city: { $exists: true }
    // }
  };

  // Initialize our service with any options it requires
  app.use('/places', service(options));

另一个注意事项,如果您尝试使用来自视图模型的内置验证功能

$select: { name: { $exists: true }}

如下所示,或添加

{$exists:true}

对于 mongoose 模型,您将获得与在 feathers-mongoose 选项中运行它相同的结果。

ready () {
       // Find all places
       placeService.find({
           query: {
               $select: { name: { $exists: true }}
           }
       }).then(page => {
         this.places = page.data
       })
   }

谢谢。

【问题讨论】:

    标签: node.js mongodb mongoose vue.js feathersjs


    【解决方案1】:

    我通过按照http://mongoosejs.com/docs/api.html#schema_string_SchemaString-minlength向猫鼬模式添加一个 minlength:1 参数解决了这个问题

    const placeSchema = new Schema({
        name: {
          type: { String, minlength: 1},
          required: true
        }
      }); 
    

    我仍然想知道这是否是最佳选择。谢谢

    【讨论】:

    • console.log查询返回的page数据了吗?它看起来像预期的那样吗?看起来您的数据不正确,并且约束修复了输出。
    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2018-02-03
    • 2012-01-13
    相关资源
    最近更新 更多