Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better fix for casting

  • Loading branch information...
commit 2d33955433f51e4c2baf7b9ebc5e44287b259375 1 parent 43c4085
@aheckmann aheckmann authored
Showing with 15 additions and 9 deletions.
  1. +2 −5 lib/mongoose/query.js
  2. +13 −4 lib/mongoose/schema/array.js
View
7 lib/mongoose/query.js
@@ -190,11 +190,8 @@ Query.prototype.cast = function (model, obj) {
$cond = ks[k];
nested = val[$cond];
- if ('$ne' === $cond) {
- if (null === nested) continue;
- if (!(Array.isArray(nested) || nested.constructor == Object)) {
- continue;
- }
+ if ('$ne' === $cond && null === $cond) {
+ continue;
}
if ('$exists' === $cond) {
View
17 lib/mongoose/schema/array.js
@@ -70,13 +70,14 @@ SchemaArray.prototype.checkRequired = function (value) {
*/
SchemaArray.prototype.cast = function (value, doc) {
- if (Array.isArray(value)){
- if (!(value instanceof MongooseArray))
+ if (Array.isArray(value)) {
+ if (!(value instanceof MongooseArray)) {
value = new MongooseArray(value, this.path, doc);
+ }
var caster = this.caster;
- if (caster)
+ if (caster) {
for (var i = 0, l = value.length; i < l; i++) {
try {
value[i] = caster.prototype.cast.call(null, value[i]);
@@ -85,6 +86,7 @@ SchemaArray.prototype.cast = function (value, doc) {
throw new CastError(e.type, value);
}
}
+ }
return value;
} else {
@@ -109,7 +111,14 @@ SchemaArray.prototype.$conditionalHandlers = {
return ArrayNumberSchema.prototype.cast.call(this, val);
}
, '$ne': function (val) {
- return this.cast(val);
+ if (Array.isArray(val)) {
+ return this.cast(val);
+ } else {
+ var proto = this.caster.prototype;
+ var method = proto.castForQuery || proto.cast;
+ if (method) return method.call(proto, val);
+ return val.toObject ? val.toObject() : val;
+ }
}
, '$in': function (val) {
return this.cast(val);
Please sign in to comment.
Something went wrong with that request. Please try again.