Skip to content

Commit

Permalink
Fix reference field error
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiel committed May 18, 2018
1 parent a003482 commit 42f4835
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
7 changes: 4 additions & 3 deletions lib/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ Sequence.prototype.getId = function() {
* reference
*/
Sequence.prototype._getCounterReferenceField = function(doc) {
var reference = [];
var reference = {};

if (this._useReference === false) {
reference = null;
}else {
for (var i in this._options.reference_fields) {
reference.push(JSON.stringify(doc[this._options.reference_fields[i]]));
reference[this._options.reference_fields[i]] = doc[this._options.reference_fields[i]];
// reference.push(JSON.stringify(doc[this._options.reference_fields[i]]));
}
}

Expand Down Expand Up @@ -153,7 +154,7 @@ Sequence.prototype._createCounterModel = function() {
CounterSchema = mongoose.Schema(
{
id: {type: String, required: true},
reference_value: {type:Array, required: true},
reference_value: { type: mongoose.Schema.Types.Mixed, required: true},
seq: {type:Number, default: 0, required: true}
},
{
Expand Down
26 changes: 13 additions & 13 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,39 +303,39 @@ describe('Basic => ', function() {

});

describe('a counter which referes others fields => ', function() {
describe('a counter which referes others fields => ', function () {

before(function() {
before(function () {
var ComposedSchema = new Schema({
country: String,
country: Schema.Types.ObjectId,
city: String,
inhabitant: Number
});
ComposedSchema.plugin(AutoIncrement, {id: 'inhabitant_counter', inc_field: 'inhabitant', reference_fields: ['country', 'city']});
ComposedSchema.plugin(AutoIncrement, { id: 'inhabitant_counter', inc_field: 'inhabitant', reference_fields: ['city', 'country'] });
this.Composed = mongoose.model('Composed', ComposedSchema);
});

it('increment on save', function(done) {
var t = new this.Composed({country:'France', city:'Paris'});
t.save(function(err) {
it('increment on save', function (done) {
var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Paris' });
t.save(function (err) {
if (err) return done(err);
assert.deepEqual(t.inhabitant, 1);
done();
});
});

it('saving a document with the same reference increment the counter', function(done) {
var t = new this.Composed({country:'France', city:'Paris'});
t.save(function(err) {
it('saving a document with the same reference increment the counter', function (done) {
var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Paris' });
t.save(function (err) {
if (err) return done(err);
assert.deepEqual(t.inhabitant, 2);
done();
});
});

it('saving with a different reference do not increment the counter', function(done) {
var t = new this.Composed({country:'USA', city:'New York'});
t.save(function(err) {
it('saving with a different reference do not increment the counter', function (done) {
var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Carcasonne' });
t.save(function (err) {
if (err) return done(err);
assert.deepEqual(t.inhabitant, 1);
done();
Expand Down

0 comments on commit 42f4835

Please sign in to comment.