javascript - mongoose validation matching an array with another array for common string? -


my mongoose schema + validation

var schemainterest = new schema({    active_elements: {     type: [string]     },    pending_elements: {   type:[string]   }   });  schemainterest.methods.matchelements = function matchelements() {   this.find({active_elements: this.pending_elements}, function(){     //shows matched elements   }); }; 

i don't know how work error handling in mongoose yet. want if elements match error return if there no match validation successful. ideas?

try adding other property in validation using this.pending_elements , comparing arrays using lodash library's _.isequal() , _.sortby() methods:

var schemainterest = new schema({       active_elements: {         type: [string]     },     pending_elements: {         type: [string]     } });  schemainterest.path('active_elements').validate(function (v) {     return _.isequal(_.sortby(v), _.sortby(this.pending_elements))   }, 'my error type'); 

-- update --

from op comments (thanks @johnnyhk pointing out), @ least 1 matching element, not whole array required need _.intersection() method creates array of unique values included in of provided arrays using samevaluezero equality comparisons:

_.intersection(v, this.pending_elements) 

would suffice. validation function this:

schemainterest.path('active_elements').validate(function (v) {     return _.intersection(v, this.pending_elements).length > 0   }, 'my error type'); 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -