Remove the undefined property to avoid mongodb upsert overwrite

This commit is contained in:
Raymond Feng 2014-05-08 15:46:39 -07:00
parent 8b128b566d
commit 4a907b0a18
1 changed files with 14 additions and 6 deletions

View File

@ -214,25 +214,33 @@ function stillConnecting(dataSource, obj, args) {
* @param {Function} callback The callback function (optional).
*/
DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (stillConnecting(this.getDataSource(), this, arguments)) {
return;
}
var Model = this;
if (!getIdValue(this, data)) return this.create(data, callback);
if (!getIdValue(this, data)) {
return this.create(data, callback);
}
if (this.getDataSource().connector.updateOrCreate) {
var inst = new Model(data);
this.getDataSource().connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
var update = inst.toObject(true);
update = removeUndefined(update);
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
var obj;
if (data) {
if (data && !(data instanceof Model)) {
inst._initProperties(data);
obj = inst;
} else {
obj = null;
obj = data;
}
callback(err, obj);
});
} else {
this.findById(getIdValue(this, data), function (err, inst) {
if (err) return callback(err);
if (err) {
return callback(err);
}
if (inst) {
inst.updateAttributes(data, callback);
} else {