Skip to content
Browse files

Merge pull request #898 from aheckmann/897

[ready] don't throw if docArray#id is called and subdoc has no _id
  • Loading branch information...
2 parents 36b05a3 + 2385a92 commit 3dd4359f5f7dd9727689f0f9909b38c6b46d530d @rauchg rauchg committed
Showing with 29 additions and 5 deletions.
  1. +9 −5 lib/types/documentarray.js
  2. +20 −0 test/types.documentarray.test.js
View
14 lib/types/documentarray.js
@@ -64,18 +64,22 @@ MongooseDocumentArray.prototype._cast = function (value) {
*/
MongooseDocumentArray.prototype.id = function (id) {
+ var casted
+ , _id;
+
try {
- var casted = ObjectId.toString(ObjectIdSchema.prototype.cast.call({}, id));
+ casted = ObjectId.toString(ObjectIdSchema.prototype.cast.call({}, id));
} catch (e) {
- var casted = null;
+ casted = null;
}
for (var i = 0, l = this.length; i < l; i++) {
- if (!(this[i].get('_id') instanceof ObjectId)) {
- if (String(id) == this[i].get('_id').toString())
+ _id = this[i].get('_id');
+ if (!(_id instanceof ObjectId)) {
+ if (String(id) == _id)
return this[i];
} else {
- if (casted == this[i].get('_id').toString())
+ if (casted == _id)
return this[i];
}
}
View
20 test/types.documentarray.test.js
@@ -103,6 +103,26 @@ module.exports = {
var a = new MongooseDocumentArray([sub3]);
a.id(id3).title.should.equal('rock-n-roll');
a.id(sub3._id).title.should.equal('rock-n-roll');
+
+ // test with no _id
+ var NoId = new Schema({
+ title: { type: String }
+ }, { noId: true });
+
+ var Subdocument = TestDoc(NoId);
+
+ var sub4 = new Subdocument();
+ sub4.title = 'rock-n-roll';
+
+ var a = new MongooseDocumentArray([sub4])
+ , threw = false;
+ try {
+ a.id('i better not throw');
+ } catch (err) {
+ threw = err;
+ }
+ threw.should.equal(false);
+
},
'inspect works with bad data': function () {

0 comments on commit 3dd4359

Please sign in to comment.
Something went wrong with that request. Please try again.