2016-08-17 62 views
0

如何在使用羽毛/ Vue堆栈的Mongoose中对查询进行验证?这是我正在经历的。传递给feathers-mongoose服务的任何查询条件必须在Vue模型中传递,以便运行查询,该查询过滤掉没有名称属性或根本没有该字段的返回项目。这是它不起作用时的样子。注意'真实'的结果。 enter image description here使用Feathersjs和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 
     }) 
    } 
}) 

如果您在您服务“选项”查询最终将显示与“真”表示{{i.name}} index.html中的项目添加此。该服务设置:

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} 

到猫鼬模型,你会得到相同的结果,如果你在羽毛 - 猫鼬选项中运行它。

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

谢谢。

回答

0

我通过增加一个固定MINLENGTH这个问题:1个参数猫鼬架构按http://mongoosejs.com/docs/api.html#schema_string_SchemaString-minlength

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

我还是想知道这是否是最佳的。谢谢

+0

你'console.log'从查询返回的页面数据?它看起来像预期的一样吗?它看起来像你有错误的数据和约束固定输出。 – Daff