mongoose-sequence
mongoose-sequence copied to clipboard
using this with two id fields is throwing errors
I've been trying to make this work with two schemas in my app and I cant seem to figure it out. I found a reference to someone else with an issue here and used their solution as a guide but its not working. I have 2 schemas set up, "userSchema" and "petSchema". Below each schema I have:
userSchema.plugin(AutoIncrement, {collection_name: 'counters'});
and
petSchema.plugin(AutoIncrement, {collection_name: 'petCounters'});
as a part of them both, the schemas have:
id: { type: Number, primaryKey: true },
But when I try to use the schemas, I get this error:
C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:170 throw new Error('Auto increment field already present and not of type "Number"'); ^
Error: Auto increment field already present and not of type "Number"
at SequenceFactory.Sequence._createSchemaKeys (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:170:13)
at SequenceFactory.Sequence.enable (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:112:10)
at SequenceFactory.Sequence.getInstance (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:95:14)
at Schema.plugin (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose\lib\schema.js:1430:3)
at Object.
Can you please help me figure out what's going wrong? I am still a beginner and I'm having a hard time figuring out what's up. I gather it's not giving the newly made pet an ID number but I can't tell why.
Can you send the schemas (relevant part) and the plugin instantiation? Can you please try to better format the code (use backtick ` or the button < >
).
Can you try to assign two different ids to the autoIncrement? Something like this
userSchema.plugin(AutoIncrement, {id: 'user_id_counter', inc_field: 'id' });
petSchema.plugin(AutoIncrement, {id: 'pet_id_counter', inc_field: 'id' });
Also, is primaryKey: true
part of mongoose :thinking: ? I think it's not, you have to define an index on the id
field if you want to simulate the effect of a primary key.