Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startAt Not working #15

Open
modermo opened this issue Feb 22, 2018 · 5 comments
Open

startAt Not working #15

modermo opened this issue Feb 22, 2018 · 5 comments

Comments

@modermo
Copy link

modermo commented Feb 22, 2018

Thank you for rescuing this package from the dead guys.

This was an issue with the previous package as well, but the startAt function doesn't work. I've also just tried saving a schema twice... and both don't increment at all. So it could be an issue stemming deeper than just startAt

@nodkz
Copy link
Owner

nodkz commented Feb 22, 2018

Tests for startAt works without errors

it('start counting at specified number', async () => {
const UserSchema = new mongoose.Schema({
name: String,
dept: String,
});
UserSchema.plugin(autoIncrement, { model: 'User', startAt: 3 });
const User = connection.model('User', UserSchema);
await User.ensureIndexes();
const user1 = new User({ name: 'Charlie', dept: 'Support' });
const user2 = new User({ name: 'Charlene', dept: 'Marketing' });
await user1.save();
await user2.save();
expect(user1._id).toBe(3);
expect(user2._id).toBe(4);
});

So it looks like identitycounters collection in your DB already has a record for your schema, which contains last count value.

StartAt option only works first time, when creating a record in identitycounters, something like this

{ "_id" : ObjectId("589b0ef632e4f434969ac3b6"), "model" : "Invoice", "field" : "invoiceNum", "count" : 93, "__v" : 0, "groupingField" : "" }

... then it will take only that count value from mongo, and no matter what startAt value is passed to your config.

So feel free to change value count in DB for your model.

@modermo
Copy link
Author

modermo commented Feb 22, 2018

I keep getting this for any record created AFTER the first. I've tried dropping both collections (the identityCounter and the affected collection), and it's still returning me this error:

E11000 duplicate key error index

@modermo
Copy link
Author

modermo commented Feb 23, 2018

So I think I know what I'm missing.

In my start.js, I have:

mongoose.connect(process.env.DATABASE);
const autoIncrement = require('mongoose-plugin-autoinc');
const mongooseConnection = mongoose.connection

And then in my model (Invoice):

invoiceSchema.plugin(autoIncrement.plugin, { 
  model: 'Invoice', 
  field: 'invoice_number',
  startAt: 2100,
  incrementBy: 2 
})

module.exports = mongoose.model('Invoice', invoiceSchema)

So I'm missing:

const Invoice = mongooseConnection.model('Invoice', invoiceSchema);

but I'm not really sure how that works, because up until now I've just been doing:

module.exports = mongoose.model('Invoice', invoiceSchema)

@modermo
Copy link
Author

modermo commented Feb 23, 2018

I'm so sorry guys, I've actually fixed it – it was my sleepy brain that was causing it. I needed to clear the index.

What I have, which works, is:

const autoIncrement = require('mongoose-plugin-autoinc');
const mongooseConnection = mongoose.connection
module.exports = mongooseConnection.model('Invoice', invoiceSchema)

Which to be honest, I'm not sure if that's the right thing to do, but it works.

@EdinsonGomez
Copy link

EdinsonGomez commented Mar 20, 2018

The same thing happens to me, but I have not solved it. they help me?

How can I reset the field if the collection is deleted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants