Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[ready] don't throw if docArray#id is called and subdoc has no _id #898

Merged
merged 1 commit into from

2 participants

@aheckmann
Owner

closes #897

@rauchg rauchg merged commit 3dd4359 into Automattic:2.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2012
  1. @aheckmann
This page is out of date. Refresh to see the latest.
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 () {
Something went wrong with that request. Please try again.