Skip to content
Browse files

Fix id:null issue #98

  • Loading branch information...
1 parent f26096a commit e10c42ed1b22f15b06638c71ea71f7fe832e4744 @1602 committed Nov 5, 2012
Showing with 10 additions and 3 deletions.
  1. +9 −3 lib/adapters/mongodb.js
  2. +1 −0 lib/adapters/mongoose.js
View
12 lib/adapters/mongodb.js
@@ -1,3 +1,4 @@
+
var safeRequire = require('../utils').safeRequire;
/**
@@ -26,7 +27,7 @@ exports.initialize = function initializeSchema(schema, callback) {
if (!s.database) s.database = url.pathname.replace(/^\//, '');
if (!s.username) s.username = url.auth && url.auth.split(':')[0];
- if (!s.password) s.password = url.auth && url.auth.split(':')[1];
+ if (!s.password) s.password = url.auth && url.auth.split(':')[1];
});
}
@@ -52,16 +53,17 @@ exports.initialize = function initializeSchema(schema, callback) {
};
function MongoDB(s, schema, callback) {
+ var i, n;
this._models = {};
this.collections = {};
var server;
if (s.rs) {
set = [];
- for(i=0, n=s.hosts.length; i<n; i++) {
+ for (i = 0, n = s.hosts.length; i < n; i++) {
set.push(new mongodb.Server(s.hosts[i], s.ports[i], {auto_reconnect: true}));
}
- server = new mongodb.ReplSetServers(set, {rs_name:s.rs});
+ server = new mongodb.ReplSetServers(set, {rs_name: s.rs});
} else {
server = new mongodb.Server(s.host, s.port, {});
@@ -102,6 +104,9 @@ MongoDB.prototype.collection = function (name) {
};
MongoDB.prototype.create = function (model, data, callback) {
+ if (data.id === null) {
+ delete data.id;
+ }
this.collection(model).insert(data, {}, function (err, m) {
callback(err, err ? null : m[0]._id.toString());
});
@@ -231,3 +236,4 @@ MongoDB.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
MongoDB.prototype.disconnect = function () {
this.client.close();
};
+
View
1 lib/adapters/mongoose.js
@@ -6,6 +6,7 @@ var safeRequire = require('../utils').safeRequire;
var mongoose = safeRequire('mongoose');
exports.initialize = function initializeSchema(schema, callback) {
+ console.error('WARN: mongoose adapter is not supported, please use "mongodb" adapter instead');
if (!mongoose) return;
if (!schema.settings.url) {

4 comments on commit e10c42e

@Mulkave
Mulkave commented on e10c42e Nov 5, 2012

This removed the "id:" field completely drom mongodb, though when the callback returns from "save" with the result, i.e. cb(err, user); user still has the id:null value.

@anatoliychakkaev
Collaborator

It should not create unexpected id:null, but when id:null already in database this is correct behavior - to show them in output, i think. If you found some other issues with this, would be great to have some test case which will demonstrate issue and we will know how exactly it should work and also prevent code from regression. So, if you have time - please create that failing test case. Thanks.

@incutonez

I know this is an older commit, but I just ran into this problem today, and when I checked my code, it had your fix in it. Then I realized that data.id was actually undefined and not null, so I added "|| data.id === undefined" to line 107 and all was well.

@anatoliychakkaev
Collaborator
Please sign in to comment.
Something went wrong with that request. Please try again.