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

using this with two id fields is throwing errors #104

Open
ponypaw opened this issue Apr 30, 2021 · 2 comments
Open

using this with two id fields is throwing errors #104

ponypaw opened this issue Apr 30, 2021 · 2 comments

Comments

@ponypaw
Copy link

ponypaw commented Apr 30, 2021

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. (C:\Users\ponyp\Desktop\MiwV2\APP.JS:61:12)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47

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.

@ramiel
Copy link
Owner

ramiel commented Apr 30, 2021

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' });

@ramiel
Copy link
Owner

ramiel commented Apr 30, 2021

Also, is primaryKey: true part of mongoose 🤔 ? 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.

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

2 participants