0

如何在 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
       })
   }

谢谢你。

4

1 回答 1

0

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

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

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

于 2016-08-17T13:21:45.787 回答