Created
May 28, 2016 18:36
-
-
Save muratatak77/bbe743e379add276cf59cc1fc90b7efb to your computer and use it in GitHub Desktop.
Mongoose Validate Array Field and expect self id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CompanySchema | |
.path('tv_broadcasters') | |
.validate(function(value, respond) { | |
var self = this; | |
this.constructor.find({ | |
tv_broadcasters: { | |
$in: value | |
}, | |
_id: { | |
$ne: self.id | |
} | |
}, function(err, company_tvs) { | |
if (err) throw err; | |
company_tvs.forEach(function(tv, key) { | |
console.log("tv_validate : ", tv); | |
console.log("self.id :", self.id); | |
console.log("tv.id :", tv._id); | |
if (self.id === tv._id) return respond(true); | |
return respond(false); | |
}) | |
respond(true); | |
}); | |
}, 'The specified tv_broadcasters is already in use.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment