Permalink
Browse files

fixed; setting empty array with model.update

closes #1838
  • Loading branch information...
1 parent 4c5f896 commit 2d1ef5569198d1cedceb9508d931b36f68a27e99 Aaron Heckmann committed Dec 18, 2013
Showing with 23 additions and 1 deletion.
  1. +1 −1 lib/schema/array.js
  2. +22 −0 test/model.update.test.js
View
@@ -124,7 +124,7 @@ SchemaArray.prototype.applyGetters = function (value, scope) {
SchemaArray.prototype.cast = function (value, doc, init) {
if (Array.isArray(value)) {
- if (!value.length) {
+ if (!value.length && doc) {
var indexes = doc.schema.indexedPaths();
for (var i = 0, l = indexes.length; i < l; ++i) {
View
@@ -900,4 +900,26 @@ describe('model: update:', function(){
});
})
});
+
+ it('casts empty arrays', function(done) {
+ var db = start();
+
+ var so = new Schema({ arr: [] });
+ var Some = db.model('1838-' + random(), so);
+
+ Some.create({ arr: ['a'] }, function (err, s) {
+ if (err) return done(err);
+
+ Some.update({ _id: s._id }, { arr: [] }, function(err) {
+ if (err) return done(err);
+ Some.findById(s._id, function(err, doc) {
+ db.close();
+ if (err) return done(err);
+ assert.ok(Array.isArray(doc.arr));
+ assert.strictEqual(0, doc.arr.length);
+ done();
+ });
+ });
+ });
+ });
});

0 comments on commit 2d1ef55

Please sign in to comment.