Skip to content

Commit

Permalink
fix arrays with undefined value
Browse files Browse the repository at this point in the history
  • Loading branch information
FloGou committed Jan 20, 2017
1 parent c340162 commit a828f85
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
10 changes: 7 additions & 3 deletions build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ function ActiveRecord(model, name) {
/** Unless it is marked nested */
!(field.nested || _.isArray(field) && field[0].nested)) {
if (_.isArray(field)) {
toSave = _this[key].map(function (entry) {
toSave = _this[key].filter(function (e) {
return !_.isUndefined(e);
}).map(function (entry) {
return typeof entry === 'string' ? entry : entry._id;
});
} else {
Expand Down Expand Up @@ -323,8 +325,10 @@ function ActiveRecord(model, name) {
* */
} else if (field.nested || _.isArray(field) && field[0].nested) {
if (_.isArray(field)) {
obj[key] = obj[key].map(function (e) {
return e.beforeSave(null, { force: true });
obj[key] = obj[key].filter(function (e) {
return !_.isUndefined(e);
}).map(function (e) {
return e.beforeSave ? e.beforeSave(null, { force: true }) : e;
});
} else {
if (obj[key] && obj[key] !== null) {
Expand Down
10 changes: 7 additions & 3 deletions dest/temp/ActiveRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ function ActiveRecord(model, name) {
/** Unless it is marked nested */
!(field.nested || _.isArray(field) && field[0].nested)) {
if (_.isArray(field)) {
toSave = _this[key].map(function (entry) {
toSave = _this[key].filter(function (e) {
return !_.isUndefined(e);
}).map(function (entry) {
return typeof entry === 'string' ? entry : entry._id;
});
} else {
Expand Down Expand Up @@ -322,8 +324,10 @@ function ActiveRecord(model, name) {
* */
} else if (field.nested || _.isArray(field) && field[0].nested) {
if (_.isArray(field)) {
obj[key] = obj[key].map(function (e) {
return e.beforeSave(null, { force: true });
obj[key] = obj[key].filter(function (e) {
return !_.isUndefined(e);
}).map(function (e) {
return e.beforeSave ? e.beforeSave(null, { force: true }) : e;
});
} else {
if (obj[key] && obj[key] !== null) {
Expand Down
8 changes: 8 additions & 0 deletions dest/temp/specs/angular-dao-base.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ describe('Angular DAO Basics', function () {
});
httpBackend.flush();
});

it('Should not fail when a nested property is undefined', function () {
var m = ModelManager.create({
_id: "111",
models2: [{ _id: "121212" }, undefined, { _id: null }, null],
model2: undefined
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "flogou <[email protected]>",
"name": "angular-orm",
"version": "2.0.0",
"version": "2.0.1",
"description": "",
"homepage": "",
"dependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/ActiveRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function ActiveRecord (model, name, SManager = SessionManager(mod
/** Unless it is marked nested */
&& !(field.nested || (_.isArray(field) && field[ 0 ].nested))) {
if (_.isArray(field)) {
toSave = this[ key ].map(entry => (typeof entry === 'string') ? entry : entry._id)
toSave = this[ key ].filter(e => !_.isUndefined(e)).map(entry => (typeof entry === 'string') ? entry : entry._id)
} else {
toSave = typeof this[ key ] === 'string' ? this[ key ] : this[ key ]._id
}
Expand Down Expand Up @@ -279,7 +279,9 @@ export default function ActiveRecord (model, name, SManager = SessionManager(mod
* */
} else if (field.nested || (_.isArray(field) && field[ 0 ].nested )) {
if (_.isArray(field)) {
obj[ key ] = obj[ key ].map(e => e.beforeSave(null, { force: true }))
obj[ key ] = obj[ key ]
.filter(e => !_.isUndefined(e))
.map(e => e.beforeSave ? e.beforeSave(null, { force: true }) : e)
} else {
if (obj[ key ] && obj[ key ] !== null) {
obj[ key ] = obj[ key ].beforeSave(null, { force: true })
Expand Down
7 changes: 7 additions & 0 deletions tst/specs/angular-dao-base.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ describe('Angular DAO Basics', function () {
expect(data).toEqual({data: []})
});
httpBackend.flush();
})

it ('Should not fail when a nested property is undefined', function(){
var m = ModelManager.create({
_id: "111",
models2: [{_id: "121212"}, undefined, {_id: null}, null],
model2: undefined
})
})

});

0 comments on commit a828f85

Please sign in to comment.