From 4a907b0a18a02ef45cc597730caaf83e7edd4618 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 8 May 2014 15:46:39 -0700 Subject: [PATCH] Remove the undefined property to avoid mongodb upsert overwrite --- lib/dao.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 267d609e..4cf85929 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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 {