Skip to content

Commit

Permalink
Fix "TypeError: Cannot read property 'indexOf' of undefined"
Browse files Browse the repository at this point in the history
Checks that `JSON.stringify(conditions[keys[z]])` is not `undefined` before calling `indexOf`.
  • Loading branch information
FdezRomero committed Jun 12, 2017
1 parent bee4241 commit 40b09a4
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions index.js
Expand Up @@ -35,15 +35,15 @@ function findOrCreatePlugin(schema, options) {
}
}
this.findOne(conditions, function(err, result) {
if(err || result) {
if(options && options.upsert && !err) {
self.update(conditions, doc, function(err, count){
if (err || result) {
if (options && options.upsert && !err) {
self.update(conditions, doc, function(err, count) {
self.findById(result._id, function(err, result) {
callback(err, result, false);
});
})
});
} else {
callback(err, result, false)
callback(err, result, false);
}
} else {
for (var key in doc) {
Expand All @@ -53,19 +53,20 @@ function findOrCreatePlugin(schema, options) {
// If the value contain `$` remove the key value pair
var keys = Object.keys(conditions);

for (var z = 0; z < keys.length; z++){
if (JSON.stringify(conditions[keys[z]]).indexOf('$') !== -1) {
for (var z = 0; z < keys.length; z++) {
var value = JSON.stringify(conditions[keys[z]]);
if (value && value.indexOf('$') !== -1) {
delete conditions[keys[z]];
}
};
}

var obj = new self(conditions)
var obj = new self(conditions);
obj.save(function(err) {
callback(err, obj, true);
});
}
})
}
});
};
}

/**
Expand Down

0 comments on commit 40b09a4

Please sign in to comment.