0

How do I use validation with queries in Mongoose with Feathers/Vue stack? This is what I am experiencing. Any query terms passed to the feathers-mongoose service has to be passed in the Vue model for it to run the query which filters out returned items that dont have a name property or dont have the field at all. This is what it looks like when it doesnt work. Notice the 'true' result.enter image description here

  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
        })
    }
})

If you add this in “options” on your service the query will end up showing the item with 'true' showing in {{ i.name }} in index.html. This the service setting:

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));

Another note, if you try to use the feathers built in validation from the view model with

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

as seen below, or by adding

{$exists:true}

to the mongoose model, you will get the same result as if you ran it in feathers-mongoose options.

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

Thank you.

armand
  • 693
  • 9
  • 29

1 Answers1

0

I fixed this problem by adding a minlength:1 parameter to the mongoose schema as per http://mongoosejs.com/docs/api.html#schema_string_SchemaString-minlength

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

I'd still like to know if this is the optimal. Thanks

armand
  • 693
  • 9
  • 29
  • Did you `console.log` the `page` data returned from the query? Does it look as expected? It looks like you had bad data and the constraint fixed the output. – Daff Aug 18 '16 at 07:33