Skip to content
This repository was archived by the owner on Jul 11, 2019. It is now read-only.

Commit

Permalink
null check fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Feb 12, 2018
1 parent 27d0efb commit 48de1a6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# 1.0.2

[Diff](https://github.com/Alorel/mongoose-transform-field-plugin/compare/1.0.1...1.0.2)

* `[fix]` Added null checks to middleware

# 1.0.1

[Diff](https://github.com/Alorel/mongoose-transform-field-plugin/compare/1.0.0...1.0.1)

* `[chore]` Tweaked formatting in the README
* `[test]` Increased test timeouts to 20 seconds @ Travis
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-transform-field-plugin",
"version": "1.0.1",
"version": "1.0.2",
"description": "An automatic field transformation plugin for Mongoose 5",
"main": "dist/index.js",
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions src/_transformSyncInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ export function transformSyncInternal(schema: Schema,
}

schema.pre('save', function(this: any): void {
if (this[source] !== undefined) {
if (this[source] !== undefined && this[source] !== null) {
this[target] = transformer(this[source]);
}
});

const preUpdate = function(this: Query<any>): void {
const upd: Update = this.getUpdate() || /* istanbul ignore next */ {};

if (upd[source] !== undefined) {
if (upd[source] !== undefined && upd[source] !== null) {
this.update({
[target]: transformer(upd[source])
});
}
if (upd.$set && upd.$set[source] !== undefined) {
if (upd.$set && upd.$set[source] !== undefined && upd.$set[source] !== null) {
this.update({
$set: {
[target]: transformer(upd.$set[source])
}
});
}
if (upd.$setOnInsert && upd.$setOnInsert[source] !== undefined) {
if (upd.$setOnInsert && upd.$setOnInsert[source] !== undefined && upd.$setOnInsert[source] !== null) {
this.update({
$setOnInsert: {
[target]: transformer(upd.$setOnInsert[source])
Expand Down
8 changes: 4 additions & 4 deletions src/transformAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function transformAsync<T>(schema: Schema,
}

const onSave = function(this: any, done: any): void {
if (this[field] !== undefined) {
if (this[field] !== undefined && this[field] !== null) {
fn(this[field])
.then((res: any) => {
this[field] = res;
Expand All @@ -63,7 +63,7 @@ function transformAsync<T>(schema: Schema,
const promises: Thenable<void>[] = [];
const upd: Update = this.getUpdate() || /* istanbul ignore next */ <any>{};

if (upd[field] !== undefined) {
if (upd[field] !== undefined && upd[field] !== null) {
promises.push(
fn(upd[field])
.then((res: any): void => {
Expand All @@ -72,7 +72,7 @@ function transformAsync<T>(schema: Schema,
);
}

if (upd.$set && upd.$set[field] !== undefined) {
if (upd.$set && upd.$set[field] !== undefined && upd.$set[field] !== null) {
promises.push(
fn(upd.$set[field])
.then((res: any): void => {
Expand All @@ -81,7 +81,7 @@ function transformAsync<T>(schema: Schema,
);
}

if (upd.$setOnInsert && upd.$setOnInsert[field] !== undefined) {
if (upd.$setOnInsert && upd.$setOnInsert[field] !== undefined && upd.$setOnInsert[field] !== null) {
promises.push(
fn(upd.$setOnInsert[field])
.then((res: any): void => {
Expand Down

0 comments on commit 48de1a6

Please sign in to comment.