These are chat archives for Automattic/mongoose

8th
Apr 2018
warlockD
@warlockdn
Apr 08 2018 08:18
Hey guys,
I have a schema with multiple index, and using the plugin 'mongoose-plugin-autoinc' to auto increment value
Now each time I am saving I get a duplicate along with this First param to schema.plugin() must be a function, got "object"
const mongoose = require('mongoose');
const autoIncrement = require('mongoose-plugin-autoinc');

// Initialize Auto Increment 
const connection = mongoose.createConnection("mongodb://localhost:27017/food");

const partnerSchema = new mongoose.Schema({
    partnerID: { type: Number, index: true, unique: true }
});

partnerSchema.plugin(autoIncrement, {
    model: 'Partner',
    field: 'partnerID',
    startAt: 100300,
    incrementBy: 3
});

const Partner = connection.model('Partner', partnerSchema);
module.exports = Partner;
can anyone suggest ?
Kev
@lineus
Apr 08 2018 09:04
hey @warlockdn :)
try: const { autoIncrement } = require('mongoose-plugin-autoinc') instead
or const autoIncrement = require('mongoose-plugin-autoinc').autoIncrement
the first way is preferable to me, as it's visually cleaner, but it's dependent on a semi-modern node. so if you're rockin node 4 or something the second syntax is valid.
warlockD
@warlockdn
Apr 08 2018 10:05
hey @lineus that did the trick. But why did we need to use the { autoIncrement } here. Can you lemme know.
Kev
@lineus
Apr 08 2018 10:09
it's because of the way the module.exports are setup in that version of the package. iow, it doesn't explicitly say module.exports = autoIncrement in that module, so you have to explicitly ask for it with one of those two versions of the require statement above.
warlockD
@warlockdn
Apr 08 2018 10:13
ah. ok :)
Kev
@lineus
Apr 08 2018 14:06
@warlockdn check out the docs on destructuring on mdn Objects are covered about half way down.
lol I guess I didn't hit enter a few hours ago when I wrote that :)
panigrah
@panigrah
Apr 08 2018 14:27
Is there a recommended authorisation plugin?
Kev
@lineus
Apr 08 2018 15:18
can you elaborate @panigrah ? do you mean a mongoose plugin?
warlockD
@warlockdn
Apr 08 2018 16:06
@lineus cool. got it
thanks man. u saved me a lot of hassle
Kev
@lineus
Apr 08 2018 16:26
:) anytime!
panigrah
@panigrah
Apr 08 2018 16:53
@lineus yes a mongoose plugin. I Have a mongoose backed graphql server.
Kev
@lineus
Apr 08 2018 16:57
I haven't ever used one, I would probably go with this one if I needed it. It's the most recently updated and the docs are clear.