2016-08-17 29 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